Damon Cortesi's blog

Musings of an entrepreneur.

Google AJAX Search API Example Python Code

| Comments

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>#!/usr/bin/python
</span><span class='line'>import urllib
</span><span class='line'>import simplejson
</span><span class='line'>
</span><span class='line'>query = urllib.urlencode({'q' : 'damon cortesi'})
</span><span class='line'>url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' \
</span><span class='line'>  % (query)
</span><span class='line'>search_results = urllib.urlopen(url)
</span><span class='line'>json = simplejson.loads(search_results.read())
</span><span class='line'>results = json['responseData']['results']
</span><span class='line'>for i in results:
</span><span class='line'>  print i['title'] + ": " + i['url']</span></code></pre></td></tr></table></div></figure></notextile></div>

Comments