调用第三方接口识别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()

 

你可能感兴趣的:(Python,爬虫)