• 搜索
  • 夜间模式
    ©2016-2025  周伯通的小站 Theme by OneBlog
    搜索
    标签
    # linux # python # android # 学习 # 折腾 # 教程 # 记录 # 软件 # vps # 新闻
  • 首页>
  • Python>
  • 正文
  • Python爬取12306登录 转载

    2018年11月18日 37 阅读 4 评论 5208 字

    转载的,就是为了学习用多点思路

    # !/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)
    本文著作权归作者 [ 老周 ] 享有,未经作者书面授权,禁止转载,封面图片来源于 [ 互联网 ] ,本文仅供个人学习、研究和欣赏使用。如有异议,请联系博主及时处理。
    python爬虫
    取消回复

    发表留言
    回复

    读者留言4

    1. หวย Lv.1
      2019-01-28 12:21 回复

      Each Midi Baccarat table will probably be limited to 18 gamers.

    2. rickyhan.biz Lv.1
      2019-02-04 23:38 回复

      Normally I do not read article on blogs, but
      I wish to say that this write-up very pressured me to check out
      and do it! Your writing taste has been surprised me. Thanks, very great post.

    3. pandora heart bracelets Lv.1
      2019-02-06 20:20 回复

      If you want to get much from this paragraph then you
      have to apply such methods to your won webpage.

    4. tienda china Lv.1
      2019-02-07 09:58 回复

      There is certainly a great deal to find out about this subject.

      I really like all the points you have made.

    加载更多评论
    加载中...
    — 已加载全部评论 —
    Copyright©2016-2025  All Rights Reserved.  Load:0.022 s
    Theme by OneBlog V3.6.4
    夜间模式

    开源不易,请尊重作者版权,保留基本的版权信息。