【问题标题】:perform a google search and return the number of results执行谷歌搜索并返回结果数
【发布时间】:2015-06-05 07:34:38
【问题描述】:

Google 网络搜索 API 似乎已失效(旧的 SOAP 和新的 AJAX)。有没有一种快速的方法可以在 Google 中搜索字符串并返回结果数?我想我只需要运行搜索并抓取结果,但我很想知道是否有更好的方法。

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

【问题讨论】:

    标签: python python-2.7 http


    【解决方案1】:

    还有a free 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
    

    结果:

    $ 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
    

    【讨论】:

    • 奇怪的是,我在尝试此操作时遇到 404 错误,即使我可以在浏览器中正常加载搜索 URL:
    • <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>404 Not Found</title> </head><body> <h1>Not Found</h1> <p>The requested URL /search was not found on this server.</p> <hr> <address>Apache/2.2.3 (Red Hat) Server at www.google.com Port 80</address> </hr></body></html>
    • 有,但如果我收到 404,我会通过代理。当我没有通过代理时,我得到一个不同的错误。
    • 问题是,这不是 Google 的 404 页面。他们的 404 页面有 Google 品牌,短语“这就是我们所知道的。”,并且没有提到 Apache 或 Red Hat。恐怕我无法进一步帮助您,只能指出您的代理设置。
    • 我确实注意到它与 Google 的标准 404 页面不同。
    猜你喜欢
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 2018-08-04
    相关资源
    最近更新 更多