Entering username & password automatically using urllib.urlopen

Ryan Ginstrom software at ginstrom.com
Sun Oct 14 23:33:59 EDT 2007


> On Behalf Of rodrigo
> I am trying to retrieve a password protected page using:
> 
> get = urllib.urlopen('http://password.protected.url"').read()

I would suggest looking at mechanize.
http://wwwsearch.sourceforge.net/mechanize/

from mechanize import Browser

USERNAME = "user"
PASSWORD = "secret"
LOGIN_PAGE = "http://password.protected.url/"

browser = Browser()
browser.open( LOGIN_PAGE )

# log in
browser.select_form( nr=0 ) # Assuming log in form is first form on the page
# Check the form for the actual field names...
browser['user'] = USERNAME
browser['pass'] = PASSWORD
browser.submit()

# Content goodness follows...

##
Of course, this assumes that the site doesn't use some kind of JavaScript
trickery to prevent automation like the above. In that case, you'd have to
use something like PAMIE
http://sourceforge.net/projects/pamie/

HTH,
Ryan Ginstrom




More information about the Python-list mailing list