Python : TypeError: 'NoneType' object has no attribute '__getitem__' -
when run python code through terminal m getting error :
def gplag(text,encode=false): import urllib, urllib2, json if encode == true: text = text.encode('utf-8') query = urllib.quote_plus(text) base_url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' url = base_url + '%22' + query + '%22' request = urllib2.request(url,none) response = urllib2.urlopen(request) result = json.load(response) output = [] if result['responsedata']['results'] != []: firstmatch = result['responsedata']['results'][0] output.append(firstmatch['title']) output.append(firstmatch['visibleurl']) output.append(firstmatch['content']) return output def callgplag(n): s in n: outcome = gplag(s,encode=true)
it gives;
typeerror: 'nonetype' object has no attribute '__getitem__'
in if result['responsedata']['results']
line. here traceback:
traceback (most recent call last): ... in <module> gplagfile(sys.argv[1]) ... in gplagfile outcome = gplag(s,encode=true) ... in gplag if 'results' in result['responsedata']: typeerror: argument of type 'nonetype' not iterable
replace line :
if result['responsedata']['results'] != []:
by
if ( len(result) , 'responsedata' in result , 'results' in result['responsedata'] , result['responsedata']['results'] != []):
Comments
Post a Comment