json - Google search with python is sporadically non-accurate and has Type Errors -
i using code found here on google search set of strings , return "expected" amount of results. here code:
for in months: b in range(1, daysinmonth[a] + 1): #code if not mystring: googlestats.append(none) else: try: query = urllib.urlencode({'q': mystring}) url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query search_response = urllib.urlopen(url) search_results = search_response.read() results = json.loads(search_results) data = results['responsedata'] googlestats.append(data['cursor']['estimatedresultcount']) except typeerror: googlestats.append(none) x in range(0, len(googlestats)): if googlestats[x] != none: finalgooglestats.append(googlestats[x]) there 2 problems, may related. when return len(finalgooglestats), it's different every time. 1 time it's 37, it's 12. however, should more 240.
this typeerror receive when take out try/except:
typeerror: 'nonetype' object has no attribute '__getitem__' which occurs on line
googlestats.append(data['cursor']['estimatedresultcount']) so, can't figure out why number of nones in googlestats changes every time , it's never low should be. if has ideas, i'd love hear them, thanks!
update
when try print out data every think i'm searching, ton of nones , very, few actual json dictionaries. dictionaries spread out across searches, don't see pattern in none , isn't. so, problem looks has more googleapi else.
first, i'd remove try..except clause , see problem is. general practice, when try access layers of dictionary elements, use .get() method instead better control.
as demonstration of possible typeerror, here educated guess:
>>> = {} >>> a['lol'] = none >>> a['lol']['teemo'] traceback (most recent call last): file "<stdin>", line 1, in <module> typeerror: 'nonetype' object has no attribute '__getitem__' >>> there ways use .get(), simple demonstration:
>>> = {} >>> b = a.get('lol') # return none >>> if type(b) dict: # determine type ... print b.get('teemo') # same technique if b indeed of type dict ... >>>
Comments
Post a Comment