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

作者:闵开慧 时间:2021-06-16 18:34:29 

市场上很多火车票抢票软件大家应该非常熟悉,但很少有人研究具体是怎么实现的,所以觉得很神秘,其实很简单。下面使用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()

来源:https://my.oschina.net/mkh/blog/1605966

标签:python,火车票,抢票
0
投稿

猜你喜欢

  • PHP json_encode中文乱码解决方法

    2023-07-12 20:20:14
  • Python 面向对象编程详解

    2023-06-25 05:24:23
  • PyTorch学习笔记之回归实战

    2023-09-17 10:26:19
  • 用Python实现网易云音乐的数据进行数据清洗和可视化分析

    2023-07-03 18:53:00
  • 仿淘宝星级评分效果

    2010-09-03 18:37:00
  • python日期相关操作实例小结

    2021-07-14 18:39:13
  • 一文搞懂​​​​​​​python可迭代对象,迭代器,生成器,协程

    2023-08-22 18:45:13
  • 妄想or未来?界面的虚拟现实化

    2010-03-01 12:53:00
  • 为自己的网站添加RSS功能

    2007-11-05 19:18:00
  • SQL 语句中的通配符

    2007-10-11 18:03:00
  • 上传组件 ASPUpload 使用说明

    2008-09-11 14:38:00
  • 下载 Firefox 3 中文版/英文版

    2008-06-19 13:27:00
  • asp常用数据库连接方法和技巧

    2010-05-27 12:28:00
  • 设计评论表单[译]

    2009-03-25 20:41:00
  • 详解duck typing鸭子类型程序设计与Python的实现示例

    2022-12-26 07:12:19
  • asp javascript值的互相传递方法

    2011-03-30 10:37:00
  • Python入门教程(八)PythonCasting用法

    2021-11-14 02:20:41
  • PHP基于rabbitmq操作类的生产者和消费者功能示例

    2023-11-23 20:03:03
  • python使用递归解决全排列数字示例

    2022-02-22 04:14:49
  • Python使用lambda表达式对字典排序操作示例

    2022-12-26 06:27:46
  • asp之家 网络编程 m.aspxhome.com