#! /usr/local/bin/python2 import socket, cStringIO, httplib from ZSI import * GoogleNS = "urn:GoogleSearch" GoogleURL = "/search/beta2" GoogleHost = 'api.google.com' class Generic: def __init__(self, name): self.name = name class tcDirCatArray(TC.Array): def __init__(self, pname=None, **kw): TC.Array.__init__(self, 'DirectoryCategory', tcDirCat(), 'directoryCategories', **kw) class tcSearchResult(TC.Struct): def __init__(self, pname=None, **kw): TC.Struct.__init__(self, Generic, [ TC.String('summary', unique=1), TC.String('URL', unique=1), TC.String('snippet', unique=1), TC.String('title', unique=1), TC.String('cachedSize', unique=1), TC.Boolean('relatedInformationPresent'), TC.String('hostName', unique=1), tcDirCat('directoryCategory'), TC.String('directoryTitle', unique=1), ], pname, inorder=0, **kw) class tcResultArray(TC.Array): def __init__(self, pname=None, **kw): TC.Array.__init__(self, 'ResultElement', tcSearchResult(), 'resultElements', **kw) class tcDirCat(TC.Struct): def __init__(self, pname=None, **kw): TC.Struct.__init__(self, Generic, [ TC.String('fullViewableName', unique=1), TC.String('specialEncoding', unique=1), ], pname, inorder=0, **kw) class tcGoogleSearchResult(TC.Struct): def __init__(self, pname=None, **kw): TC.Struct.__init__(self, Generic, [ TC.Boolean('documentFiltering'), TC.String('searchComments', unique=1), TC.Iint('estimatedTotalResultsCount'), TC.Boolean('estimateIsExact'), tcResultArray('resultElements'), TC.String('searchQuery', unique=1), TC.Iint('startIndex'), TC.Iint('endIndex'), TC.String('searchTips', unique=1), tcDirCatArray('directoryCategories'), TC.Decimal('searchTime'), ], pname, inorder=0, **kw) class tcGoogleSearch(TC.Struct): def __init__(self, pname=None, **kw): TC.Struct.__init__(self, Generic, [ TC.String('key', unique=1), TC.String('q', unique=1), TC.Iint('start'), TC.Iint('maxResults'), TC.Boolean('filter'), TC.String('restrict', unique=1), TC.Boolean('safeSearch'), TC.String('lr', unique=1), TC.String('ie', unique=1), TC.String('oe', unique=1), ], pname, inorder=0, **kw); class Search: typecode = tcGoogleSearch('g:doGoogleSearch', typed=0) def __init__(self, query, key): self.key = key self.q = query self.start = 0 self.maxResults = 10 self.filter = 1 self.restrict = '' self.safeSearch = 0 self.lr = '' self.ie = 'latin1' self.oe = 'latin1' def sendsearch(request): conn = httplib.HTTPConnection(GoogleHost, 80) conn.connect() conn.putrequest('POST', GoogleURL) conn.putheader('Content-Length', '%d' % len(request)) conn.putheader('Content-type', 'text/xml; charset="utf-8"') conn.putheader('SOAPAction', GoogleNS) conn.endheaders() conn.send(request) response = conn.getresponse() data = response.read() print response, '\n', data ps = ParsedSoap(data) results = ps.Parse(TC.Struct(Generic, [tcGoogleSearchResult('return')])) results = getattr(results, 'return') for e in results.resultElements: print e.URL # Format the request into a string. s = Search('rich+salz', 'put-your-key-here') c = cStringIO.StringIO() SoapWriter(c, nsdict={'g': GoogleNS}) \ .serialize(s, oname='doGoogleSearch') request = c.getvalue() sendsearch(request)