urllib2 data sending

孤街醉人 提交于 2019-12-24 04:07:28

问题


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

回答1:


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.




回答2:


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

Pycurl or Mechanize will make this much easier for you




回答3:


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.




回答4:


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)


来源:https://stackoverflow.com/questions/321582/urllib2-data-sending

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!