执行谷歌搜索并返回结果数量

Pur*_*ont 6 python http python-2.7

Google Web搜索API似乎已经死亡(旧的SOAP和新的AJAX).有没有快速搜索Google的字符串并返回结果数量?我假设我只需要进行搜索并搜索结果,但我很想知道是否有更好的方法.

更新:事实证明,任何不使用新API https://developers.google.com/custom-search/json-api/v1/overview的 Google自动访问都违反了他们的服务条款,因此不会推荐的.

Rob*_*obᵩ 9

还有一个免费的API,但这里有一个屏幕刮板:

import requests
from bs4 import BeautifulSoup
import argparse

parser = argparse.ArgumentParser(description='Get Google Count.')
parser.add_argument('word', help='word to count')
args = parser.parse_args()

r = requests.get('http://www.google.com/search',
                 params={'q':'"'+args.word+'"',
                         "tbs":"li:1"}
                )

soup = BeautifulSoup(r.text)
print soup.find('div',{'id':'resultStats'}).text
Run Code Online (Sandbox Code Playgroud)

结果:

$ python g.py jones
About 223,000,000 results
$ python g.py smith
About 325,000,000 results
$ python g.py 'smith and jones'
About 54,200,000 results
$ python g.py 'alias smith and jones'
About 181,000 results
Run Code Online (Sandbox Code Playgroud)