Created
January 23, 2012 02:32
-
-
Save twolfe18/1660105 to your computer and use it in GitHub Desktop.
command line tool to get URLs for a google search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys, httplib, re | |
if len(sys.argv) < 2: | |
print 'provide a query' | |
exit(0) | |
c = httplib.HTTPConnection('www.google.com') | |
c.request('GET', '/search?q=%s' % ('_'.join(sys.argv[1:]))) | |
r = c.getresponse() | |
html = r.read() | |
c.close() | |
# yes, i'm aware that HTML is a context free, not regular, language... this will do | |
p1 = re.compile('<li class="g">(.+?)</li>', re.DOTALL) | |
p2 = re.compile('<h3 class="r"><a href="(.+?)"', re.DOTALL) | |
for m1 in re.finditer(p1, html): | |
m2 = re.match(p2, m1.group(1)) | |
if m2 is not None and m2.group(1).startswith('http'): | |
print m2.group(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment