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


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

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

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

  1. # -*- coding: utf-8 -*- 
  2.  
  3. """ 
  4.  
  5. @author: liuyw 
  6.  
  7. """ 
  8.  
  9. from splinter.browser import Browser 
  10.  
  11. from time import sleep 
  12.  
  13. import traceback 
  14.  
  15. import time, sys 
  16.  
  17.  
  18.  
  19. class huoche(object): 
  20.  
  21.     """docstring for huoche""" 
  22.  
  23.     driver_name='' 
  24.  
  25.     executable_path='' 
  26.  
  27.     #用户名,密码 
  28.  
  29.     username = u"xxx@qq.com" 
  30.  
  31.     passwd = u"xxxx" 
  32.  
  33.     # cookies值得自己去找, 下面两个分别是上海, 太原南 
  34.  
  35.     starts = u"%u4E0A%u6D77%2CSHH" 
  36.  
  37.     ends = u"%u592A%u539F%2CTYV" 
  38.  
  39.     # 时间格式2018-01-19 
  40.  
  41.     dtime = u"2018-01-19" 
  42.  
  43.     # 车次,选择第几趟,0则从上之下依次点击 
  44.  
  45.     order = 0 
  46.  
  47.     ###乘客名 
  48.  
  49.     users = [u"xxx",u"xxx"] 
  50.  
  51.     ##席位 
  52.  
  53.     xb = u"二等座" 
  54.  
  55.     pz=u"成人票" 
  56.  
  57.  
  58.  
  59.     """网址""" 
  60.  
  61.     ticket_url = "https://kyfw.12306.cn/otn/leftTicket/init" 
  62.  
  63.     login_url = "https://kyfw.12306.cn/otn/login/init" 
  64.  
  65.     initmy_url = "https://kyfw.12306.cn/otn/index/initMy12306" 
  66.  
  67.     buy="https://kyfw.12306.cn/otn/confirmPassenger/initDc" 
  68.  
  69.     login_url='https://kyfw.12306.cn/otn/login/init' 
  70.  
  71.      
  72.  
  73.     def __init__(self): 
  74.  
  75.         self.driver_name='chrome' 
  76.  
  77.         self.executable_path='/usr/local/bin/chromedriver' 
  78.  
  79.  
  80.  
  81.     def login(self): 
  82.  
  83.         self.driver.visit(self.login_url) 
  84.  
  85.         self.driver.fill("loginUserDTO.user_name", self.username) 
  86.  
  87.         # sleep(1) 
  88.  
  89.         self.driver.fill("userDTO.password", self.passwd) 
  90.  
  91.         print u"等待验证码,自行输入..." 
  92.  
  93.         while True: 
  94.  
  95.             if self.driver.url != self.initmy_url: 
  96.  
  97.                 sleep(1) 
  98.  
  99.             else: 
  100.  
  101.                 break 
  102.  
  103.  
  104.  
  105.     def start(self): 
  106.  
  107.         self.driver=Browser(driver_name=self.driver_name,executable_path=self.executable_path) 
  108.  
  109.         self.driver.driver.set_window_size(1400, 1000) 
  110.  
  111.         self.login() 
  112.  
  113.         # sleep(1) 
  114.  
  115.         self.driver.visit(self.ticket_url) 
  116.  
  117.         try: 
  118.  
  119.             print u"购票页面开始..." 
  120.  
  121.             # sleep(1) 
  122.  
  123.             # 加载查询信息 
  124.  
  125.             self.driver.cookies.add({"_jc_save_fromStation": self.starts}) 
  126.  
  127.             self.driver.cookies.add({"_jc_save_toStation": self.ends}) 
  128.  
  129.             self.driver.cookies.add({"_jc_save_fromDate": self.dtime}) 
  130.  
  131.  
  132.  
  133.             self.driver.reload() 
  134.  
  135.  
  136.  
  137.             count=0 
  138.  
  139.             if self.order!=0: 
  140.  
  141.                 while self.driver.url==self.ticket_url: 
  142.  
  143.                     self.driver.find_by_text(u"查询").click() 
  144.  
  145.                     count += 1 
  146.  
  147.                     print u"循环点击查询... 第 %s 次" % count 
  148.  
  149.                     # sleep(1) 
  150.  
  151.                     try: 
  152.  
  153.                         self.driver.find_by_text(u"预订")[self.order - 1].click() 
  154.  
  155.                     except Exception as e: 
  156.  
  157.                         print e 
  158.  
  159.                         print u"还没开始预订" 
  160.  
  161.                         continue 
  162.  
  163.             else: 
  164.  
  165.                 while self.driver.url == self.ticket_url: 
  166.  
  167.                     self.driver.find_by_text(u"查询").click() 
  168.  
  169.                     count += 1 
  170.  
  171.                     print u"循环点击查询... 第 %s 次" % count 
  172.  
  173.                     # sleep(0.8) 
  174.  
  175.                     try: 
  176.  
  177.                         for i in self.driver.find_by_text(u"预订"): 
  178.  
  179.                             i.click() 
  180.  
  181.                             sleep(1) 
  182.  
  183.                     except Exception as e: 
  184.  
  185.                         print e 
  186.  
  187.                         print u"还没开始预订 %s" %count 
  188.  
  189.                         continue 
  190.  
  191.             print u"开始预订..." 
  192.  
  193.             # sleep(3) 
  194.  
  195.             # self.driver.reload() 
  196.  
  197.             sleep(1) 
  198.  
  199.             print u'开始选择用户...' 
  200.  
  201.             for user in self.users: 
  202.  
  203.                 self.driver.find_by_text(user).last.click() 
  204.  
  205.  
  206.  
  207.             print u"提交订单..." 
  208.  
  209.             sleep(1) 
  210.  
  211.             # self.driver.find_by_text(self.pz).click() 
  212.  
  213.             # self.driver.find_by_id('').select(self.pz) 
  214.  
  215.             # # sleep(1) 
  216.  
  217.             # self.driver.find_by_text(self.xb).click() 
  218.  
  219.             # sleep(1) 
  220.  
  221.             self.driver.find_by_id('submitOrder_id').click() 
  222.  
  223.             # print u"开始选座..." 
  224.  
  225.             # self.driver.find_by_id('1D').last.click() 
  226.  
  227.             # self.driver.find_by_id('1F').last.click() 
  228.  
  229.  
  230.  
  231.             sleep(1.5) 
  232.  
  233.             print u"确认选座..." 
  234.  
  235.             self.driver.find_by_id('qr_submit_id').click() 
  236.  
  237.  
  238.         except Exception as e: 
  239.  
  240.             print e 
  241.  
  242. if __name__ == '__main__': 
  243.  
  244.     huoche=huoche() 
  245.  
  246.     huoche.start()   

评论关闭