| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5250 人关注过本帖
标题:python模拟登录问题
只看楼主 加入收藏
zheng65555
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-3-17
收藏
 问题点数:0 回复次数:0 
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')


这样为什么总是登录不成功,求大神指教,哪里出问题了。
搜索更多相关主题的帖子: Windows python Chrome import 
2016-03-17 13:07
快速回复:python模拟登录问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.061316 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved