hellowolfgang hellowolfgang - 1 month ago 11
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:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11'

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')
print("url")
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
print("myUrl['unescapedUrl']")

myopener.retrieve(myUrl['unescapedUrl'],str(count)+'.jpg')

time.sleep(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

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__'
>>>
Comments