python 手动验证并模拟登录12306

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import requests
import re
import json

def login(username,password):
    #坐标参数
    code_list={
        '1':'40,40,',
        '2':'114,35,',
        '3':'192,39,,',
        '4':'257,36,',
        '5':'42,115,',
        '6':'119,107,',
        '7':'185,124,',
        '8':'272,117,'
    }
    #构造请求头
    header={'User-Agent':'Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1' }
    #验证码图片url
    img_url='https://kyfw.12306.cn/passport/captcha/captcha-image?login_site=E&module=login&rand=sjrand'
    #添加请求头
    img=session.get(img_url,headers=header)
    #获取相应头中Set-Cookie中的值
    ct=img.headers.get('Set-Cookie')
    #通过re获取_passport_session,_passport_ct,BIGipServerpool_passport
    #用于构造登录的Cookie
    param1=re.findall(r'_session=(.*);.*_ct=(.*);.*BIGipServerpool_passport=(.*);',ct)[0]
    #下载图片
    with open('code.png','wb') as f:
        f.write(img.content)

    #输入验证码位置
    code=input('请输入验证码:')
    get_code=''
    for i in code.split(','):
        get_code+=code_list.get(i)
    #验证码校验
    data={
        'answer':get_code,
        'login_site':'E',
        'rand':'sjrand'
    }
    captcha_url='https://kyfw.12306.cn/passport/captcha/captcha-check'
    r=session.post(captcha_url,data=data)
    print(r.text)
    if '验证码校验成功' in str(r.text):
        #登录
        login_url='https://kyfw.12306.cn/passport/web/login'
        #构造带Cookie的请求头
        header={'User-Agent':'Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1',
                'Cookie':'_passport_session={0}; _passport_ct={1}; RAIL_EXPIRATION=1563305746535; RAIL_DEVICEID=qVXSPD3Pfkz_eop9XFt7w58tN7IOwKdDZ_-vYEokPMoO5AYL5ATXm3Ow3crytT1qFTDNVV5C0QTxSvhRuC1udXtoTY0QDz-kBzHMxqn8MRHJaUQaWw_HQqxmI1iRGh_M_NyrKtFtNmILKsyBzg4uc1Nj-NEv7Gbt; ten_js_key=PiStDY478e%2F1JtFqAFDo%2FNmcGuz2qT3c; ten_key=sP2u2TjsxU5Fd7QPQkvsG+lTFSiWkNb5; BIGipServerpool_passport={2}; route=6f50b51faa11b987e576cdb301e545c4; BIGipServerotn=183501322.50210.0000'.format(*param1)}
        #构造参数
        data={
            'username':username,
            'password':password,
            'appid':'otn'
        }
        r=session.post(login_url,data=data,headers=header)
        print(r.text)
        if '登录成功' in  str(r.text):
            #获取uamtk值,用于构造第一次验证的Cookie
            uamtk=r.headers.get('Set-Cookie')
            uamtk=re.findall(r'uamtk=(.*);',uamtk)[0]
            #构造带Cookie的值
            header={'User-Agent':'Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1',
                    'Cookie':'_passport_session={0}; uamtk={1}; RAIL_EXPIRATION=1563305746535; RAIL_DEVICEID=qVXSPD3Pfkz_eop9XFt7w58tN7IOwKdDZ_-vYEokPMoO5AYL5ATXm3Ow3crytT1qFTDNVV5C0QTxSvhRuC1udXtoTY0QDz-kBzHMxqn8MRHJaUQaWw_HQqxmI1iRGh_M_NyrKtFtNmILKsyBzg4uc1Nj-NEv7Gbt; ten_js_key=PiStDY478e%2F1JtFqAFDo%2FNmcGuz2qT3c; ten_key=sP2u2TjsxU5Fd7QPQkvsG+lTFSiWkNb5; BIGipServerpool_passport={2}; route=6f50b51faa11b987e576cdb301e545c4; BIGipServerotn=183501322.50210.0000'.format(param1[0],uamtk,param1[2])}
            #第一次验证
            url='https://kyfw.12306.cn/passport/web/auth/uamtk'
            data={
                'appid':'otn'
            }
            r=session.post(url,data=data,headers=header)
            print(r.text)
            #第二次验证
            apptk=json.loads(r.text)['newapptk']
            url='https://kyfw.12306.cn/otn/uamauthclient'
            r=requests.post(url,data={'tk':apptk})
            print(r.text)
            return True
    return False


if __name__ == '__main__':
    session=requests.session()
    username='xxxxxxx'
    password='xxxxxxx'
    login_info=login(username,password)
    print(session,login_info)




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值