[Tutor] Open a WebPage with Forms Authentication

GoodPotatoes goodpotatoes at yahoo.com
Thu Apr 1 00:11:35 CEST 2010

I would like to sign onto a web page, but am having a difficult time determine what parameters to pass in my sign in.  For a simple test, I am trying to parse text on a "friends" status page on twitter.  This is not via the API, but the web.

import urllib2, urllib, cookielib
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
cj = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
d={'authenticity_token':'8578471445acd12dec39b3353b71c88969f1505e', 'username_or_email':'user','password':'pswd123'}
params = urllib.urlencode(d)
opener.open('http://twitter.com/login', params)

if not 'id' in [cookie.name for cookie in cj]:
    raise ValueError, "Login failed"

#########   result
Traceback (most recent call last):
  File "<pyshell#12>", line 2, in <module>
    raise ValueError, "Login failed"
ValueError: Login failed

How can I tell what the page is asking for, and if I am passing the correct parameters?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100331/d2daf6d7/attachment.html>

More information about the Tutor mailing list