May 28, 2008

Google AJAX Search API Example Python Code

For whatever reason, there aren’t many examples on the net of Python code that can be used with the Google AJAX Search API. I’m not really sure why this is and perhaps I’m missing something, but for future reference here’s some sample python code.


#!/usr/bin/python
import urllib
import simplejson

query = urllib.urlencode({'q' : 'damon cortesi'})
url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' \
  % (query)
search_results = urllib.urlopen(url)
json = simplejson.loads(search_results.read())
results = json['responseData']['results']
for i in results:
  print i['title'] + “: ” + i['url']

10 Comments so far
Leave a comment

Thank you!
your example was _very_ useful today :)
Searched about half an hour someone who could give me a soap-api key… until i found your blog.

thank you very much for this code.
I looked for something similar for a long time.
I still can’t figure out why there is only four results returned. I tried to find a clue about this limitation but i didn’t find any. Could you tell me if you have any idea about this or a place where i could find more information.

@Celon For whatever reason, the API returns 4 results at a time and only up to 16 total results for a search. You have to pass an additional “start” parameter to get the rest of the results.

You can find some more info here:
http://code.google.com/apis/ajaxsearch/documentation/reference.html#_intro_fonje

Where do I get the python library simplejson - Is there an easy way to install it?
Thanks

@moorthy If you’re running a recent version of python and have setuptools installed, you may be able to just run ‘easy_install simplejson’.

I did run into a problem with this on my MacBook, though, and had to specify an older version to install. I’ll see if I can find the link.

Downloading from http://pypi.python.org/pypi/simplejson and running through
$ python setup.py build
$ sudo python setup.py install
worked for me.

thanks a whole lot

I’m a superstar rated iReported over at CNN and I just posted an iReport on your TweetStats and TweetCloud service. Hope you like it… repost and let folks know. Maybe it’ll be featured in broadcast.

Tell folks to Twitter Rick Sanchez (CNN) about it.
http://twitter.com/ricksanchezcnn

Oh, the iReport URL about TweetStas and Tweetclouds is here:

http://www.ireport.com/docs/DOC-65484

.

This is great info. Thanks folks.



Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>