吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 13580|回复: 90
收起左侧

[Python 原创] 【原创】Python爬取12306登录

  [复制链接]
PAOLO 发表于 2018-11-12 10:05
本帖最后由 PAOLO 于 2018-11-12 10:32 编辑

在吾爱论坛这么久,第一次发帖,感觉都有点对不起论坛,闲来写了一个12306登录,小白一个,大牛勿喷!


12306登录

12306登录



[Python] 纯文本查看 复制代码
# !/usr/bin/env python
# -*- coding:utf-8 -*-
"""12306登陆"""
import requests
import base64
import re
def get_point(index):
    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',
    }
    index = index.split(',')
    temp = []
    for item in index:
        temp.append(map[item])
    return ','.join(temp)

# 实例化一个Session
session = requests.Session()  # 自动的处理cookie
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'
}
session.headers.update(headers)

# 伪装成浏览器
# 1. 访问 获取cookie
cookie_url = 'https://kyfw.12306.cn/otn/login/conf'
response = session.get(cookie_url)
# 2. 下载验证码
captcha_url = 'https://kyfw.12306.cn/passport/captcha/captcha-image64?login_site=E&module=login&rand=sjrand&1541686714134&callback=jQuery19109992892609908492_1541686371355&_=1541686371356'
response = session.get(captcha_url)
data = response.text
img_base64 = re.findall(r'"image":"(.*?)"', data)[0]
# img_base64 = 'data:image/jpg;base64,' + img_base64
# 转换成二级制数据
img_bytes = base64.b64decode(img_base64)
# 写到文件
with open('captcha.jpg', 'wb') as f:
    f.write(img_bytes)

# 3校验验证码
check_captcha = 'https://kyfw.12306.cn/passport/captcha/captcha-check?callback=jQuery19109992892609908492_1541686371355&rand=sjrand&login_site=E&_=1541686371358'
response = session.get(check_captcha, params={'answer': get_point(input('请输入正确的序号>>>:'))} )#验证码输入,请输入正确验证码的序号(不是坐标),序号之间以,号隔开,验证码在程序目录下
res = response.text
code = re.findall(r'"result_code":"(.*?)"', res)[0]
if code == '4':
    print('验证码校验成功')
    # 4 校验用户名密码
    login_url = 'https://kyfw.12306.cn/passport/web/login'
    form_data = {
        'username': '12306账号',#替换成自己的12306账号
        'password': '12306密码',#替换成自己的12306密码
        'appid': 'otn'
    }
    response = session.post(login_url, data=form_data)
    res = response.json()
    if res["result_code"] == 0:
        print('用户名密码校验成功!')
        # 5获取权限token
        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:
            print('获取token成功')
            # 6.校验 token
            check_token_url = 'https://kyfw.12306.cn/otn/uamauthclient'
            response = session.post(check_token_url, data={'tk': res['newapptk']})
            print(response.text)

免费评分

参与人数 7吾爱币 +5 热心值 +7 收起 理由
mengsiyiren + 1 + 1 谢谢@Thanks!
sunzhongxiao + 1 + 1 热心回复!
lyp701677 + 1 + 1 谢谢@Thanks!
我的心在飞 + 1 热心回复!
cc6639581 + 1 https://blog.c1993.com/?p=986 我也的12306自动登陆
lizhirui + 1 + 1 不错
yr17126 + 1 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

头像被屏蔽
cc6639581 发表于 2018-11-12 14:13
提示: 作者被禁止或删除 内容自动屏蔽
飘零的殇 发表于 2018-11-12 10:14
Mr.轻熟男 发表于 2018-11-12 10:15
z178669000 发表于 2018-11-12 10:17
来个Java版的?
易行 发表于 2018-11-12 10:18
12306明文密码登录啊,真简单
fjzays 发表于 2018-11-12 10:19
厉害了!大神
ColgateKas 发表于 2018-11-12 10:21

厉害了!大神
hsyjxp 发表于 2018-11-12 10:24
感谢楼主分享,支持一下!
 楼主| PAOLO 发表于 2018-11-12 10:24
易行 发表于 2018-11-12 10:18
12306明文密码登录啊,真简单

可以自己重新再写个文件存储账号和密码,然后引入进来,代码是为了大家方便一目了然,所以做成明文的,大神可以根据自己需要修改
飘零的殇 发表于 2018-11-12 10:26
可以来个java版的,发出来学习学习
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-23 23:07

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表