Google: Google Web APIs. SDK. Here’s how to call it from Python:

Update: ignore the code sample below; download PyGoogle, a friendly wrapper for the Google web API. Comprehensive documentation inside. Later: Early versions did not work with Python 2.2; this was fixed in 0.3. Version 0.4 adds support for doGetCachedPage and doSpellingSuggestion. Package includes updated version of SOAP.py with critical bug fixes necessary to access Google functions; remove any previous versions of SOAP.py (like the one you got from Sourceforge) from your Python library path.

Ignore the rest of this, look at the examples in PyGoogle instead…

First, get a license key. Second, download SOAP.py and put it in your library search path somewhere (like site-packages). Then call it like this:

>>> import SOAP
>>> server = SOAP.SOAPProxy('http://api.google.com/search/beta2', namespace='urn:GoogleSearch')
>>> soapfalse = SOAP.booleanType(0)
>>> data = server.doGoogleSearch( LICENSE_KEY_GOOGLE_SENT_YOU, SEARCH_STRING, 0, 10, soapfalse, '', soapfalse, '', '', '')
>>> [node._asdict for node in data.resultElements]

This gives you your search results as a list of dictionaries which each look like this (this example is the first result searching for “diveintomark”):

{u’URL’: ‘http://diveintomark.org/’,
u’cachedSize’: ‘21k’,
u’directoryCategory’: <SOAP.structType directoryCategory at 20413364>,
u’directoryTitle’: ”,
u’hostName’: ”,
u’relatedInformationPresent’: 1,
u’snippet’: ‘<b>diveintomark</b>. Monday, April 08, 2002. <b>…</b> Copyright 2002 Mark Pilgrim Powered by Movable<br> Type 2.0 Dive Into Python | Dive Into OS X | <b>diveintomark</b> | resume. ‘,
u’summary’: ”,
u’title’: ‘<b>diveintomark</b>’}

All those additional arguments passed to doGoogleSearch will be documented in the PyGoogle wrapper module I will be releasing later this evening.

Share and enjoy.

§

Respond privately

I am no longer accepting public comments on this post, but you can use this form to contact me privately. (Your message will not be published.)



§

firehosecodeplanet

© 2001–9 Mark Pilgrim