火车票抢票python代码公开揭秘!


Posted in Python onMarch 08, 2018

市场上很多火车票抢票软件大家应该非常熟悉,但很少有人研究具体是怎么实现的,所以觉得很神秘,其实很简单。下面使用Python模拟抢票程序,给大家揭秘抢票到底是怎么回事。

火车票抢票python代码公开揭秘!

该代码仅供参考,主要用于大家沟通交流,禁止用于商业用途。

具体代码如下,可以修改成自己的12306用户名账号:

# -*- coding: utf-8 -*-
from splinter.browser import Browser
from time import sleep
import traceback
import time, sys

class huoche(object):
 """docstring for huoche"""
 driver_name=''
 executable_path=''
 username = u"xxx@qq.com"
 passwd = u"xxxx"
 starts = u"%u4E0A%u6D77%2CSHH"
 ends = u"%u592A%u539F%2CTYV"
 dtime = u"2018-01-19"
 order = 0
 users = [u"xxx",u"xxx"]
 xb = u"二等座"
 pz=u"成人票"
 ticket_url = "https://kyfw.12306.cn/otn/leftTicket/init"
 login_url = "https://kyfw.12306.cn/otn/login/init"
 initmy_url = "https://kyfw.12306.cn/otn/index/initMy12306"
 buy="https://kyfw.12306.cn/otn/confirmPassenger/initDc"
 
 def __init__(self):
 self.driver_name='chrome'
 self.executable_path='/usr/local/bin/chromedriver'

 def login(self):
 self.driver.visit(self.login_url)
 self.driver.fill("loginUserDTO.user_name", self.username)
 # sleep(1)
 self.driver.fill("userDTO.password", self.passwd)
 print u"等待验证码..."
 while True:
  if self.driver.url != self.initmy_url:
  sleep(1)
  else:
  break

 def start(self):
 self.driver=Browser(driver_name=self.driver_name,executable_path=self.executable_path)
 self.driver.driver.set_window_size(1400, 1000)
 self.login()
 # sleep(1)
 self.driver.visit(self.ticket_url)
 try:
  print u"购票页面开始..."

  self.driver.cookies.add({"_jc_save_fromStation": self.starts})
  self.driver.cookies.add({"_jc_save_toStation": self.ends})
  self.driver.cookies.add({"_jc_save_fromDate": self.dtime})

  self.driver.reload()

  count=0
  if self.order!=0:
  while self.driver.url==self.ticket_url:
   self.driver.find_by_text(u"查询").click()
   count += 1
   print u"循环点击查询..." % count
   # sleep(1)
   try:
   self.driver.find_by_text(u"预订")[self.order - 1].click()
   except Exception as e:
   print e
   print u"还没开始预订"
   continue
  else:
  while self.driver.url == self.ticket_url:
   self.driver.find_by_text(u"查询").click()
   count += 1
   print u"循环点击查询..." % count
   # sleep(0.8)
   try:
   for i in self.driver.find_by_text(u"预订"):
    i.click()
    sleep(1)
   except Exception as e:
   print e
   print u"还没开始预订 %s" %count
   continue
  print u"开始预订..."
  # sleep(3)
  # self.driver.reload()
  sleep(1)
  print u'开始选择用户...'
  for user in self.users:
  self.driver.find_by_text(user).last.click()

  print u"提交订单..."
  sleep(1)
  # self.driver.find_by_text(self.pz).click()
  # self.driver.find_by_id('').select(self.pz)
  # # sleep(1)
  # self.driver.find_by_text(self.xb).click()
  # sleep(1)
  self.driver.find_by_id('submitOrder_id').click()
  # print u"开始选座..."
  # self.driver.find_by_id('1D').last.click()
  # self.driver.find_by_id('1F').last.click()

  sleep(1.5)
  print u"确认选座..."
  self.driver.find_by_id('qr_submit_id').click()

 except Exception as e:
  print e

if __name__ == '__main__':
 huoche=huoche()
 huoche.start()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
wxPython学习之主框架实例
Sep 28 Python
详谈Python3 操作系统与路径 模块(os / os.path / pathlib)
Apr 26 Python
Python 使用类写装饰器的小技巧
Sep 30 Python
解决python给列表里添加字典时被最后一个覆盖的问题
Jan 21 Python
详解Python3 pandas.merge用法
Sep 05 Python
使用python3批量下载rbsp数据的示例代码
Dec 20 Python
Python re正则表达式元字符分组()用法分享
Feb 10 Python
python绘制动态曲线教程
Feb 24 Python
Python递归调用实现数字累加的代码
Feb 25 Python
Django 404、500页面全局配置知识点详解
Mar 10 Python
Python Django中的STATIC_URL 设置和使用方式
Mar 27 Python
Python爬虫JSON及JSONPath运行原理详解
Jun 04 Python
Python实现定时备份mysql数据库并把备份数据库邮件发送
Mar 08 #Python
python实现12306抢票及自动邮件发送提醒付款功能
Mar 08 #Python
TensorFlow模型保存/载入的两种方法
Mar 08 #Python
python2.7 json 转换日期的处理的示例
Mar 07 #Python
教你用Python创建微信聊天机器人
Mar 31 #Python
为什么入门大数据选择Python而不是Java?
Mar 07 #Python
详解Python中如何写控制台进度条的整理
Mar 07 #Python
You might like
php下尝试使用GraphicsMagick的缩略图功能
2011/01/01 PHP
Win7下手动安装apache2.2、php5.4笔记
2015/04/03 PHP
php cli配置文件问题分析
2015/10/15 PHP
php实现Session存储到Redis
2015/11/11 PHP
PDO::commit讲解
2019/01/27 PHP
Javascript学习笔记6 prototype的提出
2010/01/11 Javascript
JavaScript基础知识之数据类型
2012/08/06 Javascript
js获取本机的外网/广域网ip地址完整源码
2013/08/12 Javascript
javascript生成随机大小写字母的方法
2014/02/20 Javascript
javascript中AJAX用法实例分析
2015/01/30 Javascript
jQuery实现的登录浮动框效果代码
2015/09/26 Javascript
详解JavaScript对象和数组
2015/12/03 Javascript
jQuery实现返回顶部功能
2016/02/23 Javascript
JS实现拖拽的方法分析
2016/12/20 Javascript
前端JS面试中常见的算法问题总结
2016/12/23 Javascript
深入浅析Nodejs的Http模块
2017/06/20 NodeJs
Angular实现搜索框及价格上下限功能
2018/01/19 Javascript
JS实现的JSON数组去重算法示例
2018/04/11 Javascript
手把手带你封装一个vue component第三方库
2019/02/14 Javascript
vue集成openlayers加载geojson并实现点击弹窗教程
2020/09/24 Javascript
python按照多个字符对字符串进行分割的方法
2015/03/17 Python
Python使用gensim计算文档相似性
2016/04/10 Python
Python中的FTP通信模块ftplib的用法整理
2016/07/08 Python
python自带的http模块详解
2016/11/06 Python
Python人脸识别初探
2017/12/21 Python
python生成器,可迭代对象,迭代器区别和联系
2018/02/04 Python
pycharm使用matplotlib.pyplot不显示图形的解决方法
2018/10/28 Python
Python2.7实现多进程下开发多线程示例
2019/05/31 Python
Python+OpenCV检测灯光亮点的实现方法
2020/11/02 Python
Python-split()函数实例用法讲解
2020/12/18 Python
Python 爬虫批量爬取网页图片保存到本地的实现代码
2020/12/24 Python
公司爱心捐款倡议书
2014/05/14 职场文书
《中国梦我的梦》小学生演讲稿
2014/08/20 职场文书
趣味运动会开幕词
2015/01/28 职场文书
《进击的巨人》新联动CM 兵长强势出击兽巨人
2022/04/05 日漫
MYSQL如何查看操作日志详解
2022/05/30 MySQL