Programming the Google Web API with Python

Programming the Google Web API with Python is simple and clean, as these scripts and interactive examples demonstrate.

Programming to the Google Web API from Python is a piece of cake, thanks to Mark Pilgrim’s PyGoogle wrapper module (http://diveintomark.org/projects/pygoogle/). PyGoogle abstracts away much of the underlying SOAP, XML, and request/response layers, leaving you free to spend your time with the data itself.

PyGoogle Installation

Download a copy of PyGoogle and follow the installation instructions (http://diveintomark.org/projects/pygoogle/readme.txt). Assuming all goes to plan, this should be nothing more complex than:

% python setup.py install

Alternately, if you want to give this a whirl without installing PyGoogle or don’t have permissions to install it globally on your system, simply put the included SOAP.py and google.py files into the same directory as the googly.py script itself.

The Code

#!/usr/bin/python # googly.py # A typical Google Web API Python script using Mark Pilgrim's # PyGoogle Google Web API wrapper # [http://diveintomark.org/projects/pygoogle/] # Usage: python googly.py <query> import sys, string, codecs # Use the PyGoogle module import google # Grab the query from the command-line if sys.argv[1:]: query = sys.argv[1] else: sys.exit('Usage: python googly.py <query>') # Your Google API developer's key google.LICENSE_KEY = 'insert key here' # Query Google data = google.doGoogleSearch(query) # Teach standard ...

Get Google Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.