用python进行谷歌搜索
我正在尝试运行以下脚本:用python进行谷歌搜索,python,json,Python,Json,我正在尝试运行以下脚本: 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.
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__'
Query:test
回溯(最近一次呼叫最后一次):
文件“test.py”,第7行,在
results=json['responseData']['results']
TypeError:“非类型”对象没有属性“\uuuu getitem\uuuu”
您可以打印结果,您将看到以下内容:
Google Web搜索API不再可用。请迁移到Google自定义搜索API() 试试看;)祝你好运
PS:有关更多信息,请在打印出原始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}
可以在此处找到解决方案:
Google Web搜索API不再可用。请迁移到Google自定义搜索API
json['responseData']
isNone
。看。