Urllib and login

Massi massi_srb at msn.com
Thu Sep 10 12:13:03 CEST 2009

Hi everyone, I'm trying to get data from an internet page which is
accessed from a login page. Looking at the html source code of the
login page I found that the name of the controls which handle username
and password are "login_un" and "login_pw". So I tried to write a
piece of code to access the data of the protected page:

import urllib2, urllib

opener = urllib2.build_opener()
login_data = urllib.urlencode({"login_un" : "my_un", "login_pw" :
opener.open('http://www.myprotectedsite.com/login_page', login_data)
resp = opener.open('http://www.myprotectedsite.com/hidden_page')
print resp.read()

But it doesn't work, since it prints the html code of the login page.
Can anyone point me out what I'm doing wrong?
Thanks in advance.

More information about the Python-list mailing list