2009-03-31 5 views
14

Sto tentando di utilizzare l'API di ricerca Web AJAX (JSON) di Google in Python. Sono bloccato perché urllib.urlencode() di Python prende solo coppie di valori, non stringhe da sole, per codificare. Nell'API di Google, la stringa di query è il termine di ricerca e non si associa a una variabile.L'URL codifica una coppia senza valore in Python

query = "string that needs to be encoded" 
params = urllib.urlencode(query) # THIS FAILS 
# http://code.google.com/apis/ajaxsearch/documentation/reference.html 
url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=large&%s&%s" % (params, GOOGLE_API_KEY) 

request = urllib2.Request(url) 
request.add_header('Referer', GOOGLE_REFERER) 

search_results = urllib2.urlopen(request) 
raw_results = search_results.read() 
json = simplejson.loads(raw_results) 
estimatedResultCount = json['responseData']['cursor']['estimatedResultCount'] 

if estimatedResultCount != 0: 
    print "Google: %s hits" % estimatedResultCount 

In che modo urlencode i miei termini di ricerca?

risposta