Google Search from a Python App Google Search from a Python App python python

Google Search from a Python App


There's a simple example here (peculiarly missing some quotes;-). Most of what you'll see on the web is Python interfaces to the old, discontinued SOAP API -- the example I'm pointing to uses the newer and supported AJAX API, that's definitely the one you want!-)

Edit: here's a more complete Python 2.6 example with all the needed quotes &c;-)...:

#!/usr/bin/pythonimport jsonimport urllibdef showsome(searchfor):  query = urllib.urlencode({'q': searchfor})  url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query  search_response = urllib.urlopen(url)  search_results = search_response.read()  results = json.loads(search_results)  data = results['responseData']  print 'Total results: %s' % data['cursor']['estimatedResultCount']  hits = data['results']  print 'Top %d hits:' % len(hits)  for h in hits: print ' ', h['url']  print 'For more results, see %s' % data['cursor']['moreResultsUrl']showsome('ermanno olmi')


Here is Alex's answer ported to Python3

#!/usr/bin/python3import jsonimport urllib.request, urllib.parsedef showsome(searchfor):  query = urllib.parse.urlencode({'q': searchfor})  url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query  search_response = urllib.request.urlopen(url)  search_results = search_response.read().decode("utf8")  results = json.loads(search_results)  data = results['responseData']  print('Total results: %s' % data['cursor']['estimatedResultCount'])  hits = data['results']  print('Top %d hits:' % len(hits))  for h in hits: print(' ', h['url'])  print('For more results, see %s' % data['cursor']['moreResultsUrl'])showsome('ermanno olmi')


Here's my approach to this: http://breakingcode.wordpress.com/2010/06/29/google-search-python/

A couple code examples:

    # Get the first 20 hits for: "Breaking Code" WordPress blog    from google import search    for url in search('"Breaking Code" WordPress blog', stop=20):        print(url)    # Get the first 20 hits for "Mariposa botnet" in Google Spain    from google import search    for url in search('Mariposa botnet', tld='es', lang='es', stop=20):        print(url)

Note that this code does NOT use the Google API, and is still working to date (January 2012).