Program Google in 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://pygoogle.sourceforge.net/)—now maintained by Brian Landers. 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 (http://sourceforge.net/project/showfiles.php?group_id=99616) and follow the installation instructions (http://pygoogle.sourceforge.net/dist/readme.txt). Assuming all goes to plan, this should be nothing more complex than:
% python setup.py install
Alternatively, 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
Save this code to a text file called googly.py
.
Be sure to replace insert key
here
with your own Google API key.
#!/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: ...
Get Google Hacks, 2nd Edition 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.