1

i have this code which searches for a word in google using google API, but for once it works fine but if i add many words or if i run it many times i keep getting the following error...

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

i tried searching a lot on google but couldnt know what the issue is.. can anyone please help me knowing the issue and how to handle it... was struggling with this error

 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"

thanks is advance..

1
  • Please format the code in your question properly. Copy-paste it as-is; then select the code, and press the {} button on the toolbar; alternatively, press ctrl+K. Now everyone would just get a SyntaxError from a copy-paste. Commented Mar 31, 2016 at 10:57

2 Answers 2

2

Chances are that when there are no results, jsonResponse['responseData'] is None so it has no property named results in the results or responseData itself is None (== JSON null). (The dictionary lookup fails, either for jsonResponse or jsonResponse['responseData'] being null/None.

Dump the output when that error happens to see which is None and then add a check for it before the line results = jsonResponse['responseData']['results'].

Sign up to request clarification or add additional context in comments.

4 Comments

how do i dump the output?/ i only have some knowledge in pythion.. can you suggest me one.. will be trying till then
In your case, maybe something simple like print jsonResponse after jsonResponse=json.loads(response.read()), or if you can do it on the interpreter by just entering jsonResponse. In the log run, consider using the logging module. This print/dump is temporary, just to see the cause of the error/details of the data so you can add the relevant check to your code.
i added if jsonResponse['responseData'] is not None: results = jsonResponse['responseData']['results'] and else at the last.. the word which i wanted to searched gets skipped when i use this.. is there any way to go back and search for the same word again? like goto or label??.. thanks for the reply
Instead of writing one long program, break it down into bits with functions which you will call. Like def search(word): ... with the basic search you do. Then loop the results of each, etc. Searching for a missing word will usually give you no results even the second time you search, so not sure what you're trying to do there.
0

Aneroid is correct about the response data.

One possible solution to handle this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.