Skip to content

Instantly share code, notes, and snippets.

@jpadilla
Created October 5, 2010 15:13
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 jpadilla/611708 to your computer and use it in GitHub Desktop.
Save jpadilla/611708 to your computer and use it in GitHub Desktop.
import mechanize
import cookielib
from BeautifulSoup import BeautifulSoup
# Browser
br = mechanize.Browser()
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
# Want debugging messages?
br.set_debug_http(True)
br.set_debug_redirects(True)
br.set_debug_responses(True)
# User-Agent (this is cheating, ok?)
br.addheaders = [('User-agent', 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3')]
# Open some site, let's pick a random one, the first that pops in mind:
r = br.open('https://m.bancopopular.com/cibp-web/actions/login')
# Show the html title
print br.title()
# Show the available forms
for f in br.forms():
print f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment