from googleapiclient.discovery import build
def google_results_count(query):
service = build("customsearch", "v1",
developerKey="[put your API key here]")
result = service.cse().list(
q=query,
cx='[put your CSE key here']
).execute()
return result["searchInformation"]["totalResults"]
print google_results_count('Python is awesome')
最近我也在寻找Google搜索API,被很多过时的信息误导了。这是我在谷歌开发者网站上发现的:https://developers.google.com/api-client-library/python/apis/customsearch/v1
根据文件,你的职能是
不幸的是,使用CSE API将给您提供不同于使用web搜索的结果计数。在上面的例子中,我在Python中得到了2680000,在Google.com上得到了2100000 这里有一个解释为什么:https://support.google.com/customsearch/answer/70392?hl=en
获取API和CSE密钥以及CSE的所有限制是完全不同的,我强烈建议您查看这个答案:https://stackoverflow.com/a/11206266/1704272以及下面的下一个备选答案。
另一种方法是解析来自Google.com的HTML响应,这将为您提供最完整的结果,但它不是非常可靠,因为Google更改了HTML标记。更重要的是,这与他们的任务大纲背道而驰,这里要读的更多:Is it ok to scrape data from Google results?
我的结论。 你有三个选择:
相关问题 更多 >
编程相关推荐