Created
April 2, 2011 04:56
-
-
Save theepicsnail/899241 to your computer and use it in GitHub Desktop.
Google via mechanize
This file contains 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
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