执行谷歌搜索并返回结果数量 [英] perform a google search and return the number of results
问题描述
Google Web搜索API似乎已经死亡(旧的SOAP和新的AJAX)。有没有快速搜索Google的字符串并返回结果数量?我假设我只需要进行搜索并搜索结果,但我很想知道是否有更好的方法。
The Google Web Search APIs appear to be dead (both the old SOAP and the newer AJAX). Is there a quick way to search Google for a string and return the number of results? I assume I just have to run the search and scrape the results, but I'd love to know if there's a better way.
更新:事实证明,任何不使用新API的Google自动访问 https://developers.google.com/custom-search/json-api/v1/overview 违反了他们的服务条款,因此不推荐。
Update: It turns out that any automated access to Google that doesn't use their new API https://developers.google.com/custom-search/json-api/v1/overview violates their terms of service, and is thus not recommended.
推荐答案
仍有一个免费的API ,但这里是一个屏幕刮刀:
There is still a free API, but here is a screen-scraper:
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
结果:
$ 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
这篇关于执行谷歌搜索并返回结果数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!