我试图在脚本下面运行:
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__'
答案 0 :(得分:2)
使用以下代码打印出原始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?
答案 1 :(得分:0)
您可以打印结果,您将看到以下内容:
Google Web Search API不再可用。请迁移到Google自定义搜索API(https://developers.google.com/custom-search/)
试试;)祝你好运
PS:有关详细信息,您可以访问另一个堆栈溢出问题中的this detailed tutorial