Skip to content

Instantly share code, notes, and snippets.

@theepicsnail
Created April 2, 2011 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theepicsnail/899241 to your computer and use it in GitHub Desktop.
Save theepicsnail/899241 to your computer and use it in GitHub Desktop.
Google via mechanize
import mechanize
br = mechanize.Browser()
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
def doSearch(term):
global br
br.open("http://www.google.com")
br.select_form(name="f")
br["q"]=term
resp = br.submit()
return list(br.links())[39] #currently the 40th link is the first result
if __name__ == "__main__":
print "Type anything into the prompt to get googles first link!"
print "Press return without anything typed, to quit"
while True:
x = raw_input("google>")
if not x:
exit(0)
link = doSearch(x)
print link.text
print link.url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment