tags:

views:

649

answers:

3

I'm trying to run a google search query from a python app. Is there any python interface out there? If there isn't does anyone know which Google API will enable me to do this. THanks

+14  A: 
Alex Martelli
+3  A: 

Here is Alex's answer ported to Python3

#!/usr/bin/python3
import json
import urllib.request, urllib.parse

def 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')
gnibbler
A: 

Thank you so much! I was stuck converting from 2.6 to 3.1 on this small point for hours!

bsg