Skip to content

Instantly share code, notes, and snippets.

@AndrewRayCode
Created November 14, 2011 03:29
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 AndrewRayCode/1363166 to your computer and use it in GitHub Desktop.
Save AndrewRayCode/1363166 to your computer and use it in GitHub Desktop.
Google search query
import twill.commands
import BeautifulSoup
import time
class browser:
def __init__(self, url="http://www.google.com",log = None):
self.a=twill.commands
self.a.config("readonly_controls_writeable", 1)
self.b = self.a.get_browser()
self.b.set_agent_string("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14")
self.log = log
self.b.clear_cookies()
self.url=url
def googleQuery(self, query="Default Query, not important"):
self.b.go(self.url)
f = self.b.get_form("f")
f["q"] = query
self.b.clicked(f, "btnG")
self.b.submit()
pageContent = self.b.get_html()
soup=BeautifulSoup.BeautifulSoup(pageContent)
ths = soup.findAll(attrs={ "class" : "r"}, limit=1)
for a in ths:
print a.findAll('a')[0]['href']
t=browser()
t.googleQuery("94103 San Francisco California \"File a complaint\"")
time.sleep(1)
t.googleQuery("94102 San Francisco California \"File a complaint\"")
time.sleep(1)
t.googleQuery("94102 San Francisco California \"File a complaint\"")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment