2

I've recently written this with help from SO. Now could someone please tell me how to make it actually log onto the board. It brings up everything just in a non logged in format.

import urllib2, re
import urllib, re
logindata = urllib.urlencode({'username': 'x', 'password': 'y'})
page = urllib2.urlopen("http://www.woarl.com/board/index.php", logindata)
pagesource = page.read()
print pagesource
Brian Tompsett - 汤莱恩
5,92772 gold badges64 silver badges135 bronze badges
asked Nov 26, 2008 at 17:56
1
  • What's the status that comes back with the page? The page object is rather complex, and has status codes and headers. What are those? Commented Nov 26, 2008 at 17:58

4 Answers 4

5

Someone recently asked the same question you're asking. If you read through the answers to that question you'll see code examples showing you how to stay logged in while browsing a site in a Python script using only stuff in the standard library.

The accepted answer might not be as useful to you as this other answer, since the accepted answer deals with a specific problem involving redirection. However, I recommend reading through all of the answers regardless.

answered Nov 26, 2008 at 18:06
Sign up to request clarification or add additional context in comments.

Comments

3

You probably want to look into preserving cookies from the server.

Pycurl or Mechanize will make this much easier for you

answered Nov 26, 2008 at 18:02

Comments

1

If actually look at the page, you see that the login link takes you to http://www.woarl.com/board/ucp.php?mode=login

That page has the login form and submits to http://www.woarl.com/board/ucp.php?mode=login again with POST.

You'll then have to extract the cookies that are probably set, and put those in a CookieJar or similar.

answered Nov 26, 2008 at 18:08

Comments

0

You probably want to create an opener with these handlers and apply it to urllib2. With these applied your cookies are handled and you'll be redirected, if server decides it wants you somewhere else.

# Create handlers
cookieHandler = urllib2.HTTPCookieProcessor() # Needed for cookie handling
redirectionHandler = urllib2.HTTPRedirectHandler() # needed for redirection (not needed for javascript redirect?)
# Create opener
opener = urllib2.build_opener(cookieHandler,redirectionHandler)
# Install the opener
urllib2.install_opener(opener)
answered Nov 27, 2008 at 3:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.