python - TypeError when retrieving Google indexes for keywords -
we found code via google. supposed give google indexes keywords. problem works while gives error:
./g1.py size hassize traceback (most recent call last): file "./g1.py", line 22, in <module> n2 = int(gsearch(args[0]+" "+args[1])['cursor']['estimatedresultcount']) typeerror: 'nonetype' object unsubscriptable
the code:
#!/usr/bin/env python import math,sys import json import urllib def gsearch(searchfor): query = urllib.urlencode({'q': searchfor}) 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'] return data args = sys.argv[1:] m = 45000000000 if len(args) != 2: print "need 2 words arguments" exit n0 = int(gsearch(args[0])['cursor']['estimatedresultcount']) n1 = int(gsearch(args[1])['cursor']['estimatedresultcount']) n2 = int(gsearch(args[0]+" "+args[1])['cursor']['estimatedresultcount'])
i ran code few times , found every once in while query returns object has data['responsedata'] = none
. causing error reported. find out why case, output entire data
object when data['responsedata'] = none
, , found following:
{u'responsedata': none, u'responsedetails': u'suspected terms of service abuse. please see http://code.google.com/apis/errors', u'responsestatus': 403}
it appears of requests returning http 403 forbidden
status codes, , result query not being fulfilled (and has no data). read the page on google's terms of service , see if can figure out why of requests may violating terms of service.
Comments
Post a Comment