python模拟登录问题
#-*-coding:utf-8-*-import urllib2
import urllib
import cookielib
import re
def getToken(opener,url):
header = {
'Host': 'login.',
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36',
'DNT': 1,
'Accept-Language': 'zh-CN,zh;q=0.8',
'Cookie': '__guid=3537848.262938988660372670.1457430766852.798; Q=u%3Dmurat65555%26n%3D%26le%3D%26m%3D%26qid%3D370265639%26im%3D220255dq9816%26src%3Dpcw_cloud%26t%3D1; T=s%3D99629954944c93bbac25bb08d3210987%26t%3D1458111878%26lm%3D%26lf%3D4%26sk%3D08042ed6a0466575fcf99dfb7fc10a40%26mt%3D1458111878%26rc%3D%26v%3D2.0%26a%3D0'
}
rq = urllib2.Request(url,headers=header)
result = opener.open(rq)
r = (""token":"(.*?)"")
token = re.findall(r,bytes(result.read().decode('utf-8')))
return token
def getOpener():
cj = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj),urllib2.HTTPHandler)
return opener,cj
def doLogin(opener,url,token):
data = {
'src':'pcw_cloud',
'from':'pcw_cloud',
'charset':'UTF-8',
'requestScema':'https',
'o':'sso',
'm':'login',
'lm':0,
'captFlag':1,
'rtype':'data',
'validatelm':0,
'isKeepAlive':'',
'captchaApp':'i360',
'userName':'帐号',
'type':'normal',
'account':'账号',
'password':'密码',
'captcha':'',
'token':token,
'proxy':'http://yunpan.',
'callback':'QiUserJsonp24000592',
'func':'QiUserJsonp24000592'
}
header = {
'Host': 'login.',
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Origin': 'http://yunpan.',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0',
'Content-Type': 'application/x-www-form-urlencoded',
'DNT': 1,
'Referer': 'http://yunpan.',
'Accept-Language': 'zh-CN,zh;q=0.8'
}
postdata = urllib.urlencode(data)
request = urllib2.Request(url,postdata,header)
result = opener.open(request)
return result
def test():
c = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(c))
header = {
'Host': 'login.',
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36',
'DNT': 1,
'Accept-Language': 'zh-CN,zh;q=0.8',
'Cookie': '__guid=3537848.262938988660372670.1457430766852.798; Q=u%3Dmurat65555%26n%3D%26le%3D%26m%3D%26qid%3D370265639%26im%3D220255dq9816%26src%3Dpcw_cloud%26t%3D1; T=s%3D99629954944c93bbac25bb08d3210987%26t%3D1458111878%26lm%3D%26lf%3D4%26sk%3D08042ed6a0466575fcf99dfb7fc10a40%26mt%3D1458111878%26rc%3D%26v%3D2.0%26a%3D0'
}
rq = urllib2.Request('https://login.',headers=header)
result = opener.open(rq)
print result.read()
print c._cookies
if __name__ == '__main__':
url='https://login.'
urlToken = 'https://login.'
resulturl = 'http://c59.yunpan.'
opener,cj = getOpener()
token = getToken(opener,urlToken)
print token[0]
result = doLogin(opener,url,token[0])
print result.read().decode('utf-8')
cg = opener.open(resulturl)
print cg.read().decode('utf-8')
这样为什么总是登录不成功,求大神指教,哪里出问题了。