urllib2 login help

Joshua Kugler joshua at joshuakugler.com
Mon Feb 23 14:33:45 EST 2009


You also might want to look at http://wwwsearch.sourceforge.net/mechanize/ 
It will do most of the hard stuff for you (sending form elements, keeping
track of cookies, etc.).

j

john.weatherwax at gmail.com wrote:

> Hello,
> 
> I'm having trouble using urllib2 (maybe) when trying to log into a web
> site that requires a user to enter a login name and a password
> (authentication).  I've tried many things but none seem to work and
> have become stuck recently and was hoping to get a hint from those
> much more knowledgeable than myself.
> 
> I want to automate logging on to the investopedia stock simulator web
> site.
> 
> http://simulator.investopedia.com/authorization/login.aspx
> 
> I can't seem to automate this successfully.
> 
> My python script is below:
> 
> import os,re
> import urllib, urllib2
> 
> theurl = "http://simulator.investopedia.com/authorization/login.aspx"
> 
> post_dict = { "ct100$MainPlaceHolder$usernameText": "XXX",
> "ct100$MainPlaceHolder$passwordText": "XXX", "ct100$MainPlaceHolder
> $loginButton": "Sign In", "ct100$MainPlaceHolder$rememberMeCheckBox":
> "on" }
> 
> post_data = urllib.urlencode( post_dict )
> 
> headers = { 'User-agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows
> NT)',
>             'Host': 'simulator.investopedia.com',
>             'Referer': 'http://simulator.investopedia.com/
> authorization/login.aspx',
>             }
> 
> req = urllib2.Request( theurl, post_data, headers )
> response = urllib2.urlopen(req)
> the_page = response.read()
> 
> The problem is that this page returned seems to be the same as the
> login page which I connected to initially.  When I perform the login
> process manually things work as expected.  What am I doing
> incorrectly?
> 
> *ANY* hints/suggestions/directions would be very appreciated since
> I've run out of ideas of things to try at this point.
> 
> Thanks very much
> 
> 
> --
> http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list