Searching from Python

9 views
Skip to first unread message

wrybread

unread,
Nov 12, 2008, 4:17:37 PM11/12/08
to Google AJAX APIs
I'm trying to do some google searches from Python, and I'm told the
way to do it is via the Ajax API from non javascript environments:

http://code.google.com/apis/ajaxsearch/documentation/#fonje

It works well, but I'm having trouble processing the results. The
problem is the results are in the form of a Python dictionary, but
Python reads them as a string, and I can't figure out how to return
the results to a dictionary. Here's my code, if anyone wants to have a
look:

import urllib2

url = "http://ajax.googleapis.com/ajax/services/search/web?
v=1.0&q=Paris%20Hilton"

req = urllib2.Request(url)
opener = urllib2.build_opener()
data = opener.open(req).read()

# replace the "null" with "None" for Python
data = data.replace(": null,", ": None,")

# how to convert the string response to a dictionary?

# the code below doesn't work, since its still a string....

# simplify the results a bit
results = data["responseData"]["results"]

for i in results:
print i["url"]


wrybread

unread,
Nov 12, 2008, 4:17:37 PM11/12/08
to Google AJAX APIs

Jeremy Geerdes

unread,
Nov 12, 2008, 4:40:29 PM11/12/08
to Google-AJAX...@googlegroups.com
A couple of links for parsing JSON into Python (from http://
www.json.org ):

http://docs.python.org/library/json.html
http://pypi.python.org/pypi/simplejson/

Jeremy R. Geerdes
Effective website design & development
Des Moines, IA

For more information or a project quote:
http://jgeerdes.home.mchsi.com
jgee...@mchsi.com

Unless otherwise noted, any price quotes contained within this
communication are given in US dollars.

If you're in the Des Moines, IA, area, check out Debra Heights
Wesleyan Church!

wrybread

unread,
Nov 12, 2008, 4:47:38 PM11/12/08
to Google AJAX APIs
Thanks, this looks great. I solved it using some duct tape before
getting your response, figured I'd update my code just in case someone
finds it useful. To anyone else reading this, this is a kludgey
method, you should probably use the json method posted by Jeremy.

With that said:

import urllib2

url = "http://ajax.googleapis.com/ajax/services/search/web?
v=1.0&q=Paris%20Hilton"

req = urllib2.Request(url)
opener = urllib2.build_opener()
data = opener.open(req).read()

# replace the "null" with "None" for Python
data = data.replace(": null,", ": None,")

# convert the string to a dictionary
exec("data = " + data)

wrybread

unread,
Nov 12, 2008, 4:51:33 PM11/12/08
to Google AJAX APIs
But I'm still only getting 4 results... I don't imagine anyone has a
way to request more results? I tried changing the url to:

url = "http://ajax.googleapis.com/ajax/services/search/web?
v=1.0&q=Paris%20Hilton&num=30"

But it still only sends 4 results.

Jeremy Geerdes

unread,
Nov 12, 2008, 4:54:35 PM11/12/08
to Google-AJAX...@googlegroups.com
Did you see my response to your other thread?

http://groups.google.com/group/Google-AJAX-Search-API/browse_thread/thread/407a1bf2a2219117

Jeremy R. Geerdes
Effective website design & development
Des Moines, IA

For more information or a project quote:
http://jgeerdes.home.mchsi.com
jgee...@mchsi.com

Unless otherwise noted, any price quotes contained within this
communication are given in US dollars.

If you're in the Des Moines, IA, area, check out Debra Heights
Wesleyan Church!

Reply all
Reply to author
Forward
0 new messages