TypeError:“NoneType”对象在使用google API的python代码中没有属性“\uuuu getitem\uuuuuu”

TypeError:“NoneType”对象在使用google API的python代码中没有属性“\uuuu getitem\uuuuuu”,python,json,Python,Json,我有这段代码,它使用谷歌API在谷歌中搜索一个单词,但有一次它工作得很好,但是如果我添加了很多单词,或者如果我多次运行它,我会不断得到以下错误 results = jsonResponse['responseData']['results'] TypeError: 'NoneType' object has no attribute '__getitem__' 我试着在谷歌上搜索了很多,但不知道问题出在哪里。。谁能帮助我了解这个问题以及如何处理它。。。他正在努力克服这个错误 impo

我有这段代码,它使用谷歌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。字典查找失败,因为jsonResponse或jsonResponse['responseData']为null/None

当该错误发生时,转储输出以查看哪个错误为无,然后在results=jsonResponse['responseData']['results']行之前添加对该错误的检查。

有关响应数据的信息是正确的

处理此问题的一个可能解决方案:

responseData = jsonResponse['responseData']
if responseData is not None:
    results = responseData['results']
    for results in results:
        # your code
else:
    print "No Response"   

请正确设置问题中的代码格式。复制粘贴原样;然后选择代码,按工具栏上的{}按钮;或者,按ctrl+K。现在每个人都会从复制粘贴中得到一个语法错误。我如何转储输出?/我只有一些pythion方面的知识。。你能给我推荐一个吗。。在您的例子中,可能会尝试一些简单的方法,比如在jsonResponse=json.loadsresponse.read之后打印jsonResponse,或者您可以在解释器上输入jsonResponse来完成。在日志运行中,考虑使用。此打印/转储是临时的,只是为了查看错误的原因/数据的详细信息,以便您可以将相关检查添加到代码中。我添加了如果jsonResponse['responseData']不是None:results=jsonResponse['responseData']['results'],最后是else。。当我使用这个时,我想搜索的单词被跳过了。。有没有办法回去再次搜索同一个单词?像goto或label??。。感谢您的回复,不要编写一个长程序,而是使用您将调用的函数将其分解为若干位。像def searchword:。。。使用您所做的基本搜索。然后循环搜索每个单词的结果,等等。即使在第二次搜索时,搜索一个缺少的单词通常也不会得到结果,因此不确定您要在那里做什么。