We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

A Stupid Google Suggest Hack

Fredrik Lundh | December 2004 | Originally posted to online.effbot.org

import urllib

URI = "http://www.google.com/complete/search?hl=en&js=true&qu="

def suggest(term):
    # get the javascript dataset
    text = urllib.urlopen(URI + urllib.quote(term)).read()
    # pretend it's python
    text = text.replace("new Array", "new_Array").rstrip().rstrip(";")
    # simulate the "google suggest" javascript environment
    dict = {}
    dict["frameElement"] = None
    dict["new_Array"] = dict["sendRPCDone"] = lambda *x: x
    # and run it
    return eval(text, dict)

print suggest("python")

(since it’s using a bare eval, it’s probably not a good idea to use this in production code…)

update: if you want a slightly more usable data structure, you can replace the last line in the function with:

    return zip(*eval(text, dict)[2:4])

which gives you something like:

('python', '80,300,000 results'),
('python tutorial', '19,700,000 results'),
('pythons', '1,840,000 results'),
('python ide', '2,400,000 results'),
('python list', '73,900,000 results'),
('python string', '2,240,000 results'),
('python dictionary', '1,500,000 results'),

update: Tokuhiro Masuno has written a more robust, RE-based version: PyGoogleSuggest (but what’s with that staticmethod-in-class thingie? make it a function already!)