Python 实现12306登录功能实例代码


Posted in Python onFebruary 09, 2018

下面一段代码给大家带来了python实现12306登录功能,具体代码如下所示:

#!/usr/bin/env python
import requests
import urllib.parse
import random
import time
req = requests.session()
import sys
import re
import urllib3
import getpass # 密文输入
urllib3.disable_warnings()
# 登陆-------------------------
apptklist = []
newStrList = []
def signin():
 ran = random.uniform(1, 0)
 imgUrl = ('https://kyfw.12306.cn/passport/captcha/captcha-image?'
    'login_site=E&module=login&rand=sjrand&%s' % ran)
 print(imgUrl)
 imgresponse = req.get(url=imgUrl, verify=False)
 codeimg = imgresponse.content
 fn = open('code.png', 'wb')
 fn.write(codeimg)
 fn.close()
 codeStr = input('请输入验证码的坐标:')
 a = ''
 b = ''
 c = ''
 d = ''
 e = ''
 f = ''
 g = ''
 h = ''
 if '1' in codeStr:
  a = '37,37,'
 if '2' in codeStr:
  b = '100,37,'
 if '3' in codeStr:
  c = '180,37,'
 if '4' in codeStr:
  d = '250,37,'
 if '5' in codeStr:
  e = '37,100,'
 if '6' in codeStr:
  f = '100,100,'
 if '7' in codeStr:
  g = '180,100,'
 if '8' in codeStr:
  h = '250,100,'
 newCodeStr = a+b+c+d+e+f+g+h
 newStr = newCodeStr[:-1]
 newStrList.append(newStr)
 url = 'https://kyfw.12306.cn/passport/captcha/captcha-check'
 data = {
  'answer':newStr,
  'login_site':'E',
  'rand':'sjrand'
 }
 headers = {
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
      ' Chrome/63.0.3239.108 Safari/537.36',
 }
 response = req.post(url=url, data=data, headers=headers, verify=False)
 print('检测图片-----------', url)
 print(response.text)
 result = response.json()
 if result['result_code'] == '4':
  print('验证码校验成功')
 else:
  print('验证码校验失败,请注意填写正确的坐标')
  signin()
  return
 userName = input('Please input your userName:')
 # password = input('Please input your password:')
 password = getpass.getpass('Please input your password:')
 loginData = {
  'username':userName,
  'password':password,
  'appid':'otn'
 }
 headers = {
  'Host':'kyfw.12306.cn',
  'Referer':'https://kyfw.12306.cn/otn/login/init',
  'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2)'
      ' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 '
      'Safari/537.36'
 }
 response = req.post('https://kyfw.12306.cn/passport/web/login', data=loginData, headers=headers, verify=False)
 print('--------------登陆中--------------------')
 print('https://kyfw.12306.cn/passport/web/login')
 print('response', response.text.encode('utf-8').decode('utf-8'))
 fd = open("locate.html", 'wb+')
 fd.write(response.content)
 yzData = {
  'appid':'otn'
 }
 response = req.post('https://kyfw.12306.cn/passport/web/auth/uamtk', data=yzData, headers=headers, verify=False)
 print('---------------------第一次验证---------------------')
 print(response.text)
 # print('typeof response',type(response))
 loginMessage = response.json()['newapptk']
 print('loginMessage=', loginMessage)
 # 第二次验证开始++++++++++++++++++++++++++++++
 yz2Data = {
  'tk': loginMessage
 }
 response = req.post('https://kyfw.12306.cn/otn/uamauthclient', data=yz2Data, headers=headers,verify=False)
 print('---------------------第二次验证---------------------')
 print(response.text)
 apptk = response.json()['apptk']
 apptklist.append(apptk)
def buy():
 print(newStrList)
 req.headers['Referer'] = 'https://kyfw.12306.cn/otn/leftTicket/init'
 result = req.post('https://kyfw.12306.cn/otn/login/checkUser')
 print('----------------购票系统--------------')
 print(result.text)
 print('验证登录状态成功checkUser')
 headers = {
  'Referer':'https://kyfw.12306.cn/otn/leftTicket/init',
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36'
      ' (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
 }
 data = {
  # 'secretStr':reserve[1],
  'train_date':'2018-01-17',
  'back_train_date':'2018-01-17',
  'tour_flag':'dc', # dc 单程
  'purpose_codes':'ADULT', # adult 成人票
  'query_from_station_name':'成都',
  'query_to_station_name':'长沙',
  'undefined':''
 }
def ticket():
 # 先登陆、然后查询车票信息
 signin()
 url = ('https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date=2018-02-10&'
   'leftTicketDTO.from_station=SZQ&leftTicketDTO.to_station=NFG&purpose_codes=ADULT')
 try:
  response = requests.get(url, verify=False)
  result = response.json()
  print(result)
  return result['data']['result']
 except Exception as e:
  return None
if __name__ == "__main__":
 # ticket()
 # with open('./aaa.xlsx', encoding='utf-8') as f:
 #
 #  print(f.read())
 dic = {}
 context = [('IDS_ABOUT_OFFICAL_PHONE', 'Službeni telefon'),
    ('IDS_ABOUT_OFFICAL_WEBSITE', 'Službeno web-mjesto'),
    ('IDS_ABOUT_OFFICIAL_PHONE_CALL_NOT_SUPPORT', 'Trenutni uređaj ne može pozivati.')]
 for i in range(len(context)):
  print(i)
  dic.setdefault(context[i][0],context[i][1])
 print(dic)
 print(dic.keys())

总结

以上所述是小编给大家介绍的Python 实现12306登录实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
Python实现PS滤镜功能之波浪特效示例
Jan 26 Python
Python 创建空的list,以及append用法讲解
May 04 Python
selenium+python自动化测试之环境搭建
Jan 23 Python
Python实现将HTML转成PDF的方法分析
May 04 Python
Python 实现数据结构-堆栈和队列的操作方法
Jul 17 Python
Python使用grequests(gevent+requests)并发发送请求过程解析
Sep 25 Python
Python 异步协程函数原理及实例详解
Nov 13 Python
Python 实现取多维数组第n维的前几位
Nov 26 Python
python实现ftp文件传输系统(案例分析)
Mar 20 Python
简述 Python 的类和对象
Aug 21 Python
Python特殊属性property原理及使用方法解析
Oct 09 Python
深入理解python多线程编程
Apr 18 Python
Python多层装饰器用法实例分析
Feb 09 #Python
理论讲解python多进程并发编程
Feb 09 #Python
Python装饰器(decorator)定义与用法详解
Feb 09 #Python
详解python的ORM中Pony用法
Feb 09 #Python
python监控键盘输入实例代码
Feb 09 #Python
Python with语句上下文管理器两种实现方法分析
Feb 09 #Python
Python遍历pandas数据方法总结
Feb 09 #Python
You might like
php设计模式 Factory(工厂模式)
2011/06/26 PHP
PHP+iFrame实现页面无需刷新的异步文件上传
2014/09/16 PHP
PHP计算百度地图两个GPS坐标之间距离的方法
2015/01/09 PHP
php多进程中的阻塞与非阻塞操作实例分析
2020/03/04 PHP
jQuery 源码分析笔记(7) Queue
2011/06/19 Javascript
jquery实现的图片点击滚动效果
2014/04/29 Javascript
js淡入淡出焦点图幻灯片效果代码分享
2015/09/08 Javascript
jquery基于layui实现二级联动下拉选择(省份城市选择)
2017/06/20 jQuery
nodejs之get/post请求的几种方式小结
2017/07/26 NodeJs
vue2.0设置proxyTable使用axios进行跨域请求的方法
2017/10/19 Javascript
关于Vue背景图打包之后访问路径错误问题的解决
2017/11/03 Javascript
最实用的JS数组函数整理
2017/12/05 Javascript
基于Vue 2.0 监听文本框内容变化及ref的使用说明介绍
2018/08/24 Javascript
ES6 Set结构的应用实例分析
2019/06/26 Javascript
浅析Vue下的components模板使用及应用
2019/11/27 Javascript
Python中input和raw_input的一点区别
2014/10/21 Python
Python生成随机密码
2015/03/10 Python
如何利用Python分析出微信朋友男女统计图
2019/01/25 Python
PyQt5下拉式复选框QComboCheckBox的实例
2019/06/25 Python
python 字典访问的三种方法小结
2019/12/05 Python
Pytorch中实现只导入部分模型参数的方式
2020/01/02 Python
关于windows下Tensorflow和pytorch安装教程
2020/02/04 Python
Python列表解析操作实例总结
2020/02/26 Python
python中的yield from语法快速学习
2020/11/06 Python
html5 canvas绘制放射性渐变色效果
2018/01/04 HTML / CSS
探讨HTML5移动开发的几大特性(必看)
2015/12/30 HTML / CSS
澳大利亚网上买书:Angus & Robertson
2019/07/21 全球购物
优秀员工自荐书
2013/12/19 职场文书
群众路线教育实践活动方案
2014/02/02 职场文书
简历上的自我评价
2014/02/03 职场文书
上班玩手机检讨书
2014/02/17 职场文书
人力资源部经理助理岗位职责
2014/03/04 职场文书
贫困证明模板(3篇)
2014/09/16 职场文书
事业单位聘任报告
2015/03/02 职场文书
linux下导入、导出mysql数据库命令的实现方法
2021/05/26 MySQL
Windows server 2016服务器基本设置
2022/08/14 Servers