Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Search from a Python App

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

like image 444
Res Avatar asked Nov 01 '09 16:11

Res


People also ask

Can you use Python to search Google?

googlesearch is a Python library for searching Google, easily. googlesearch uses requests and BeautifulSoup4 to scrape Google.

How do you make a search engine like Google in Python?

Create a file urls.py in the engine folder. Append the following lines. Our project is now done , to fire it up type python3 manage.py runserver enter this url in your browser and you should see this. Now enter your query in the search bar and your should get your results like this.


2 Answers

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/python import json import urllib  def 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') 
like image 192
Alex Martelli Avatar answered Sep 22 '22 07:09

Alex Martelli


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') 
like image 32
John La Rooy Avatar answered Sep 24 '22 07:09

John La Rooy