调用第三方接口识别12306验证码并自动登陆

import time
import json
import base64
import random
import requests
from bs4 import BeautifulSoup

def get_pic_point(image):
    
    point_map = {
        '1': '37,46',
         '2': '110,46',
         '3': '181,46',
         '4': '253,46',
         '5': '37,116',
         '6': '110,116',
         '7': '181,116',
         '8': '253,116',
        }
    
    url= "http://littlebigluo.qicp.net:47720/"
    headers   = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',}
    files={'pic_xxfile':('image.png',image,'image/png'),}
    
    res=requests.post(url,headers = headers,files=files)
    if res.status_code == 200:
        soup = BeautifulSoup(res.text,'lxml')
        points = soup.select_one('font').text
        return ','.join([point_map[point] for point in points.split()])
    else:
        return None


def get_point_360(imgbase64):
    url       = "http://60.205.200.159/api"
    headers   = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',}
    data      = {"base64":imgbase64,}
    res       = requests.post(url,headers = headers,json = data).json()
    check     = res['check']
    data      = {
        '=':'',
        'check':check,
        'img_buf':imgbase64,
        'logon':1,
        'type':'D',}
    url = "http://check.huochepiao.360.cn/img_vcode"
    res =  requests.post(url,json = data,headers = headers).json()
    res = res['res']
    res = res.replace('(','')
    res = res.replace(')','')
    return res


def check_captcha():
    captcha_url = 'https://kyfw.12306.cn/passport/captcha/captcha-image64'

    headers   = {
            'Host': 'kyfw.12306.cn',
            'Referer': 'https://kyfw.12306.cn/otn/leftTicket/init',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
            }
    
    random_data = random.random()

    session = requests.Session()

    params = {
        'login_site':'E',
        'module':'login',
        'rand':'sjrand',
        '_':random_data,
        }
    response = session.get(captcha_url,params = params,headers = headers)
    try:
        data = response.json()
        img_base64 = data['image']
    except:
        print ("验证码获取失败!")
        return
    
    #points = get_point_360(img_base64)
    points = get_pic_point(base64.b64decode(img_base64))
    params.pop('module')
    params['_'] = int(1000 * time.time())
    params['answer'] = points
    
    check_captcha = 'https://kyfw.12306.cn/passport/captcha/captcha-check'
    response = session.get(check_captcha,params = params,headers = headers)

    data = response.json()
    if data['result_code'] != '4':
        print ("验证码识别失败!")
        return
    
    form_data = {  
            'username': "*************",   #12306账号
            'password': "*************",   #12306密码
            'appid': 'otn'
            }
    login_url = 'https://kyfw.12306.cn/passport/web/login'
    response = session.post(login_url, data = form_data)
    res = response.json()
    if res["result_code"] == 0:
        uamtk_url = 'https://kyfw.12306.cn/passport/web/auth/uamtk'
        response = session.post(uamtk_url, data={'appid': 'otn'})
        res = response.json()
        if res["result_code"] == 0:
            check_token_url = 'https://kyfw.12306.cn/otn/uamauthclient'
            response = session.post(check_token_url, data={'tk': res['newapptk']})
            data = response.json()
            print ("{0}!欢迎你:{1}!".format(data['result_message'],data['username']))
    
if __name__ == '__main__':
   check_captcha()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值