编程语言
首页 > 编程语言> > python – Google Search API – 仅返回4个结果

python – Google Search API – 仅返回4个结果

作者:互联网

经过大量实验和谷歌搜索后,以下Python代码成功调用了Google的搜索APi – 但只返回4个结果:在阅读Google Search API文档后,我认为’start =’会返回其他结果:但这不会发生.

任何人都可以指点吗?谢谢.

Python代码:

/usr/bin/python
import urllib
import simplejson

query = urllib.urlencode({'q' : 'site:example.com'})
url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s&start=50' \
  % (query)
search_results = urllib.urlopen(url)
json = simplejson.loads(search_results.read())
results = json['responseData']['results']
for i in results:
  print i['title'] + ": " + i['url']

解决方法:

启动选项不会为您提供更多结果,它只会向您推进那么多结果.将结果视为队列.从50开始将给出结果50,51,52和53.

有了这个,您可以通过开始每第4个结果获得更多结果:

import urllib
import simplejson

num_queries = 50*4 
query = urllib.urlencode({'q' : 'example'})
url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query

for start in range(0, num_queries, 4):
    request_url = '{0}&start={1}'.format(url, start)
    search_results = urllib.urlopen(request_url)
    json = simplejson.loads(search_results.read())
    results = json['responseData']['results']
    for i in results:
        print i['title'] + ": " + i['url']

标签:google-search,python,google-search-api
来源: https://codeday.me/bug/20191006/1858399.html