views:

51

answers:

1

Hi, I want to be able to authenticate to a website and then access some of the private pages in that site. I've looked at some examples and tutorials but I can't get it to work.

For example, I want to access https://www.billmonk.com/home which is available only after authentication. Here's the code I'm using:

url = 'https://www.billmonk.com/home'
values = {'usercontact' : '[email protected]',
          'password' : 'somepass'}

data = urllib.urlencode(values)
req = urllib2.Request(url, data)

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)

response = opener.open(req)
the_page = response.read()

This doesn't seem to be working. I always get a page with a "You must be logged in to access this page" page.

Am I missing something obvious?

Thanks!

+1  A: 

Looking at the source of the BillMonk page, it looks like the login action is a POST to /sign_in (not /home as your code uses).

Dave Bacher
Hey, it worked! In my case I needed first to go to /sign_in and then go to /home to get the page I'm interested.Thanks!
Roger Bill