-1

这是我的python代码。我想用python登录这个网站,但无法实现。

 import sys, time, os, re
 import urllib, urllib2, cookielib

 loginurl = 'https://auth.dxy.cn/login?null'
 #loginurl = 'https://www.douban.com/accounts/login'

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

 params = {
         'username' : 'XXXX',
         'password' : 'XXXX',
         'lt' : '_c573C07C4-AD13-CD25-85E9-CC917AF0433F_k42B35CAA-C2E7-7B2C-A7A1-9FD4E6CBE30F',
         '_eventId' : 'submit'
         }

 req = urllib2.Request(loginurl,urllib.urlencode(params))
 res = urllib2.urlopen(req).read()
 m = re.search(r'\s+(.+?)\s+',str(cookie))
 jsession=re.sub(r'\s+', '', m.group(0))
 #print jsession

 header={'Cookie':jsession}
 #response=opener.open(req).read()
 req1 = urllib2.Request(loginurl,urllib.urlencode(params),header)
 res1 = urllib2.urlopen(req1)
 print res1.geturl()

 response1=opener.open(res1.geturl())
 print response1.read()
4

1 回答 1

0

由于用户代理问题,我以前也遇到过类似的问题。尝试正常登录(通过您选择的浏览器)并查看发送的任何标题信息。尝试在你的脚本中模仿它。

如果您不知道如何设置标头,请查看:如何将标头添加到 urllib2 开启程序?

于 2012-08-21T08:46:52.713 回答