[英]TypeError when searching google with the google api python 3
import requests, urllib, json
def search(search_string):
query = urllib.parse.urlencode({'q': search_string})
url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query
search_response = urllib.request.urlopen(url)
search_results = search_response.read().decode("utf8")
results = json.loads(search_results)
data = results['responseData']
print('Total results: %s' % data['cursor']['estimatedResultCount'])
hits = data['results']
print('Top %d hits:' % len(hits))
for h in hits: print(' ', h['url'])
print('For more results, see %s' % data['cursor']['moreResultsUrl'])
print(hits)
search(input('Enter search query:'))
我使用了在本網站上找到的這段代碼,當我輸入一些內容進行搜索時會返回此錯誤:
print('Total results: %s' % data['cursor']['estimatedResultCount'])
TypeError: 'NoneType' object is not subscriptable
...我不確定為什么。
該消息表示您正在嘗試訪問None
值的鍵,類似於None['key']
。
這意味着data
或data['cursor']
為空。 您應該調試這些以查看它們的值。 data
為空,因為您的請求有誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.