0

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!

2
  • 2
    Cookies aside (which I don't have cause to muck with in Python), some sites have been known to reject Python HTTP requests because they don't like the User-agent field. Commented Jul 7, 2010 at 17:17
  • 2
    @msw: From Python docs, "some HTTP servers only allow requests coming from common browsers as opposed to scripts". Interesting. Looks like there's a workaround, though. "headers should be a dictionary, and will be treated as if add_header() was called with each key and value as arguments. This is often used to “spoof” the User-Agent header, which is used by a browser to identify itself". From docs.python.org/library/urllib2.html#urllib2.Request or docs.python.org/py3k/library/… Commented Jul 7, 2010 at 17:26

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.