By Sean Reifschneider Date 2004-12-13 16:27 Tags python, sean reifschneider
Fredrik Lundh has a stupid googlesuggest hack, which I've tweaked to be a simple command-line program:
guin:tmp$ googlesuggest python
python (10,600,000 results)
python tutorial (2,630,000 results)
pythons (460,000 results)
python 2.2 (1,840,000 results)
python programming (4,380,000 results)
[...]
Simply save this code out to a file and run it, giving the search strings you want on the command-line.
#!/usr/bin/env python
import sys, string
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 zip(*eval(text, dict)[2:4])
res = suggest(string.join(sys.argv[1:], ' '))
if not res: sys.exit(0)
collen = max(map(lambda x: len(x[0]), res))
for term, count in res:
print '%-*s (%s)' % ( collen, term, count )
Google is extremely cool. If you have seen Google Suggest, what are you waiting for?
comments powered by Disqus