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']
Continue reading » · Written on: 05-28-08 · 14 Comments »

14 Responses to “Google AJAX Search API Example Python Code”

  1. Wiedi wrote:

    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.

    May 28th, 2008 at 8:26 am
  2. Cel wrote:

    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.

    June 10th, 2008 at 7:07 am
  3. Damon wrote:

    @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

    June 10th, 2008 at 2:38 pm
  4. moorthy wrote:

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

    June 17th, 2008 at 11:14 am
  5. Damon wrote:

    @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.

    June 17th, 2008 at 1:04 pm
  6. Damon wrote:

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

    July 13th, 2008 at 5:19 pm
  7. anonymous wrote:

    thanks a whole lot

    July 29th, 2008 at 9:27 pm
  8. Dan Hollings wrote:

    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

    August 24th, 2008 at 4:45 pm
  9. Dan Hollings wrote:

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

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

    .

    August 24th, 2008 at 4:47 pm
  10. Joe Dorocak wrote:

    This is great info. Thanks folks.

    August 26th, 2008 at 12:46 pm
  11. brad wrote:

    very useful, thanks.

    September 14th, 2008 at 2:26 am
  12. Andrew Fogg wrote:

    Brilliant, brilliant, brilliant! :-)

    September 24th, 2008 at 4:47 pm
  13. rhea wrote:

    thank you so much… I would also like to know how to modify the query in this code to use special syntax in the search query.. for eg. blue sky (azure OR turquoise)..

    December 13th, 2008 at 5:12 am
  14. rhea wrote:

    Actually i am getting different results when directly using the google site to search and when i use this python code to query google. How do i get the results to match?

    December 13th, 2008 at 6:00 am

Leave a Reply