Python: 404 Error when trying to login a webpage by using 'urllib' and 'HTTPCookieProcessor'

KMeans Algorithm bilbaow at gmail.com
Sun Jan 12 07:17:01 EST 2014


I'm trying to log in a webpage by using 'urllib' and this piece of code

---------
import urllib2,urllib,os

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
login = urllib.urlencode({'username':'john', 'password':'foo'})
url = "https://www.mysite.com/loginpage"
req = urllib2.Request(url, login)
try:
    resp = urllib2.urlopen(req)
    print resp.read()
except urllib2.HTTPError, e:
    print ":( Error = " + str(e.code)
----------------

But I get a "404" error (Not Found). The page "https://www.mysite.com/loginpage" does exist (note please the httpS, since I'm not sure if this the key of my problem).

If I try with

-------
resp = urllib2.urlopen(url)   
--------
(with no 'login' data), it works ok but, obviously, I'm not logged in.

What am I doing wrong? Thank you very much.



More information about the Python-list mailing list