Skip to content

Google from command line

by admin on January 5th, 2012
#!/usr/bin/env python

__author__ = "Tom Luo"
__version__ = "$Revision 0.1 $"
__date__ ="$Date: 2012/01/04 11:11PM MST$"

import os, urllib, sys
import re

def f(x):
    return x.find('Cached') < 0

filename = 'http://www.google.com/search?' + urllib.urlencode({'q': ' '.join(sys.argv[1:]) })
fobj= os.popen("lynx -dump -width=120 %s" % filename)
output = fobj.read()
fobj.close()
start = output.find("Web Results 1 - 10")
end = output.find("Searches related to:")
lines = output[start:end]
lines = reversed(re.split('\[\d+\]',lines))
lines = filter(f,lines)
print '.'.join(lines)
chmod +x g.sh
g.sh oracle

From → Linux, Python

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS