hellowolfgang hellowolfgang - 1 year ago 185
Python Question

another TypeError: 'NoneType' object is unsubscriptable

So, i found this piece of magic some time ago and it worked perfectly

import os
import sys
import time
from urllib import FancyURLopener
import urllib
import urllib2
import json as simplejson

searchTerm = "somethingsomething"

searchTerm = searchTerm.replace(' ','%20')

class MyOpener(FancyURLopener):
version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv: Gecko/20071127 Firefox/'

myopener = MyOpener()

count= 0

for i in range(0,10):
url = ('https://ajax.googleapis.com/ajax/services/search/images?' + 'v=1.0&q='+searchTerm+'&start='+str(i*4)+'&userip=MyIP')
request = urllib2.Request(url, None, {'Referer': 'testing'})
response = urllib2.urlopen(request)

results = simplejson.load(response)
data = results['responseData']
dataInfo = data['results']

for myUrl in dataInfo:
count = count + 1



few days ago i wanted to use it again and got this error:

dataInfo = data['results']
TypeError: 'NoneType' object is unsubscriptable

I am using the same version of python as before (2.6)
I don't know how to fix this, is it api or what, anyone?

(To clarify: I am using this to my artistic project, I don't know how to "really" code, so please guys, be gentle with me ;) I know that there is similar question to mine, but I don't know how to apply answer to my problem...)

Answer Source

You code is not correctly indented. Also, results['responseData'] is None with a responseDetails that says This API is no longer available. So, you do not have results['responseData']['results'] at all.

>>> url = 'https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=somethingsomething&start=36&userip=MyIP'
>>> request = urllib2.Request(url, None, {'Referer': 'testing'})
>>> response = urllib2.urlopen(request)
>>> results = simplejson.load(response)
>>> print(results)
{u'responseData': None, u'responseDetails': u'This API is no longer available.', u'responseStatus': 403}
>>> data = results['responseData']
>>> dataInfo = data['results']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object has no attribute '__getitem__'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download