【问题标题】:get the first 10 google results using googleapi使用 google api 获取前 10 个 google 结果
【发布时间】:2011-05-25 10:00:49
【问题描述】:

我需要获得前 10 个 google 搜索结果

例如:

... query = urllib.urlencode({'q' : 'example'})
... 
... url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' \
... % (query)
... search_results = urllib.urlopen(url)
... json = simplejson.loads(search_results.read())
... results = json['responseData']['results']

这会给我第一页的结果,但我想获得更多的谷歌结果,有人知道怎么做吗?

【问题讨论】:

    标签: python json google-api


    【解决方案1】:

    我过去做过,这里是完整的例子(我不是 python 大师,但它有效):

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import sys, getopt
    import urllib
    import simplejson
    
    OPTIONS = ("m:", ["min="])
    
    def print_usage():
        s = "usage: " + sys.argv[0] + " "
        for o in OPTIONS[0]:
            if o != ":" : s += "[-" + o + "] "
        print(s + "query_string\n")
    
    def search(query, index, offset, min_count, quiet=False, rs=[]):
        url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=large&%s&start=%s" % (query, offset)
        result = urllib.urlopen(url)
        json = simplejson.loads(result.read())
        status = json["responseStatus"]
        if status == 200:
            results = json["responseData"]["results"]
            cursor = json["responseData"]["cursor"]
            pages = cursor["pages"]
            for r in results:
                i = results.index(r) + (index -1) * len(results) + 1
                u = r["unescapedUrl"]
                rs.append(u)
                if not quiet:
                    print("%3d. %s" % (i, u))
            next_index  = None
            next_offset = None
            for p in pages:
                if p["label"] == index:
                    i = pages.index(p)
                    if i < len(pages) - 1:
                        next_index  = pages[i+1]["label"]
                        next_offset = pages[i+1]["start"]
                    break
            if next_index != None and next_offset != None:
                if int(next_offset) < min_count:
                    search(query, next_index, next_offset, min_count, quiet, rs)
        return rs
    
    def main():
        min_count = 64
        try:
            opts, args = getopt.getopt(sys.argv[1:], *OPTIONS)
            for opt, arg in opts:
                if opt in ("-m", "--min"):
                    min_count = int(arg)
            assert len(args) > 0
        except:
            print_usage()
            sys.exit(1)
        qs = " ".join(args)
        query = urllib.urlencode({"q" : qs})
        search(query, 1, "0", min_count)
    
    if __name__ == "__main__":
        main()
    

    编辑:我已经修复了明显的命令行选项错误处理;您可以按如下方式调用此脚本:

    python gsearch.py --min=5 vanessa mae
    

    --min 开关表示“至少 5 个结果”并且是可选的,如果未指定,您将获得最大允许结果数 (64)。

    此外,为简洁起见,省略了错误处理。

    【讨论】:

    【解决方案2】:

    请参阅文档http://code.google.com/apis/websearch/docs/reference.html#_intro_fonje

    您正在寻找开始参数。

    没有参数可以在一个响应中获得更多结果,但您可以通过 start 参数进行迭代。

    【讨论】:

      猜你喜欢
      • 2013-07-20
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 2020-04-30
      • 1970-01-01
      • 2011-10-21
      相关资源
      最近更新 更多