我urllib2在Python中使用将登录数据发布到网站.
成功登录后,该站点将我的请求重定向到另一个页面.有人可以提供一个关于如何在Python中执行此操作的简单代码示例urllib2吗?我想当我被重定向到另一个页面时,我还需要登录 cookie .对?
非常感谢advace.
首先,获得机械化:http://wwwsearch.sourceforge.net/mechanize/
您可以使用urllib2来完成这类工作,但是您将编写大量的样板代码,而且它将是错误的.
然后:
import mechanize
br = mechanize.Browser()
br.open('http://somesite.com/account/signin/')
br.select_form('loginForm')
br['username'] = 'jekyll'
br['password'] = 'bananas'
br.submit()
# At this point, you're logged in, redirected, and the
# br object has the cookies and all that.
br.geturl() # e.g. http://somesite.com/loggedin/
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用浏览器对象br并执行您必须执行的操作,单击链接等.检查机械化站点上的示例
| 归档时间: |
|
| 查看次数: |
606 次 |
| 最近记录: |