【问题标题】:Googlesearch with python使用 python 进行谷歌搜索
【发布时间】:2017-04-29 10:15:29
【问题描述】:

我正在尝试运行以下脚本:

import urllib
import json as m_json
query = raw_input ( 'Query: ' )
query = urllib.urlencode ( { 'q' : query } )
response = urllib.urlopen ( 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&' + query ).read()
json = m_json.loads ( response )
results = json [ 'responseData' ] [ 'results' ]
for result in results:
    title = result['title']
    url = result['url']   # was URL in the original and that threw a name error exception
    print ( title + '; ' + url )

我的输出是:

Query: test
Traceback (most recent call last):
  File "test.py", line 7, in <module>
    results = json [ 'responseData' ] [ 'results' ]
TypeError: 'NoneType' object has no attribute '__getitem__'

【问题讨论】:

  • Google Web Search API 不再可用。请迁移到 Google 自定义搜索 API
  • json['responseData']None。见here

标签: python json


【解决方案1】:

使用以下代码打印出原始 json 后:

print(str(json))

我们从谷歌得到以下回复:

{'responseData': None, 'responseDetails': 'The Google Web Search API is no longer available. Please migrate to the Google Custom Search API (https://developers.google.com/custom-search/)', 'responseStatus': 403}

可以在这里找到解决方案:

What are the alternatives now that the Google web search API has been deprecated?

【讨论】:

    【解决方案2】:

    您可以打印结果,您将看到以下内容:

    Google Web Search API 不再可用。请迁移到 Google 自定义搜索 API (https://developers.google.com/custom-search/)

    试试看 ;) 祝你好运

    PS:如需了解更多信息,您可以在另一个堆栈溢出问题中访问this detailed tutorial

    【讨论】:

      猜你喜欢
      • 2013-06-26
      • 2014-05-02
      • 1970-01-01
      • 2011-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多