gpt4 book ai didi

python - 类型错误 : 'NoneType' object has no attribute '__getitem__' in python code which uses google API

转载 作者:行者123 更新时间:2023-12-01 04:02:02 27 4
gpt4 key购买 nike

我有这段代码,它使用谷歌API在谷歌中搜索一个单词,但是一次它工作正常,但是如果我添加很多单词或者如果我运行它很多次,我就会不断收到以下错误...

    results = jsonResponse['responseData']['results']
TypeError: 'NoneType' object has no attribute '__getitem__'

我尝试在谷歌上搜索很多,但不知道问题是什么..任何人都可以帮助我了解这个问题以及如何处理它...正在努力解决这个错误

 import urllib
import urllib2
from urllib import urlencode
import json as m_json
from urllib2 import urlopen
import re
import json
from nltk.corpus import stopwords
import sys
from urllib2 import urlopen
import urllib2
import simplejson
import pprint

words = ['headache','diabetes','myopia','dhaed','snow','blindness','head','ache','acne','aids','blindness','head','ache','acne','aids','blindness','head','ache','acne','aids']


for word in words:
url = ('https://ajax.googleapis.com/ajax/services/search/web'
'?v=1.0&q='+word+'&userip=192.168.1.105')
request = urllib2.Request(url)
response = urllib2.urlopen(request)
jsonResponse=json.loads(response.read())
#print "the response now is: ",jsonResponse
#pprint.pprint(jsonResponse)
results = jsonResponse['responseData']['results']
for result in results:
print "\nthe result is: ",result
url =result['url']
print "\nthe url is: ",url
try:
page=urllib2.urlopen(url).read()
except urllib2.HTTPError,err:
if err.code == 403:
print "bad"
continue
else:
print "good"
break
except urllib2.HTTPError:
print "server error"
except:
print "dont know the error"

谢谢先..

最佳答案

有可能当没有结果时,jsonResponse['responseData']None所以它没有名为 results属性在结果中或 responseData本身就是None (== JSON null )。 (字典查找失败,因为 jsonResponsejsonResponse['responseData'] 为 null/None。

当错误发生时转储输出,看看哪个是 None然后在 results = jsonResponse['responseData']['results'] 行之前添加一个检查.

关于python - 类型错误 : 'NoneType' object has no attribute '__getitem__' in python code which uses google API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36331091/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com