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 »
Thank you!
May 28th, 2008 at 8:26 amyour 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.
June 10th, 2008 at 7:07 amI 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:
June 10th, 2008 at 2:38 pmhttp://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?
June 17th, 2008 at 11:14 amThanks
@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 pmDownloading from http://pypi.python.org/pypi/simplejson and running through
July 13th, 2008 at 5:19 pm$ python setup.py build
$ sudo python setup.py install
worked for me.
thanks a whole lot
July 29th, 2008 at 9:27 pmI’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.
August 24th, 2008 at 4:45 pmhttp://twitter.com/ricksanchezcnn
Oh, the iReport URL about TweetStas and Tweetclouds is here:
http://www.ireport.com/docs/DOC-65484
.
August 24th, 2008 at 4:47 pmThis is great info. Thanks folks.
August 26th, 2008 at 12:46 pmvery useful, thanks.
September 14th, 2008 at 2:26 amBrilliant, brilliant, brilliant!
September 24th, 2008 at 4:47 pmthank 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 amActually 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