你的浏览器不支持canvas

墨染半纸,清心煮字

用Python自动抢火车票

Date: Author: 吕雄

本文章采用 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议 进行许可。转载请注明来自吕雄


# -*- coding: utf-8 -*-
"""
Created on Fri Feb 23 12:31:27 2018

@author: lenovo
"""

from splinter.browser import Browser
from time import sleep
import traceback

driver = webdriver.Chrome('C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe')

class Buy_Tickets(object):
    # 定义实例属性,初始化
    def __init__(self, username, passwd, order, passengers, dtime, starts, ends):
        self.username = username
        self.passwd = passwd
        # 车次,0代表所有车次,依次从上到下,1代表所有车次,依次类推
        self.order = order
        # 乘客名
        self.passengers = passengers
        # 起始地和终点
        self.starts = starts
        self.ends = ends
        # 日期
        self.dtime = dtime
        YW =['硬卧'] 
        self.YW = YW
        self.login_url = 'https://kyfw.12306.cn/otn/login/init'
        self.initMy_url = 'https://kyfw.12306.cn/otn/index/initMy12306'
        self.ticket_url = 'https://kyfw.12306.cn/otn/leftTicket/init'
        self.driver_name = 'chrome'
        self.executable_path = 'C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe'
    # 登录功能实现
    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)
        # sleep(1)
        print('请输入验证码...')
        while True:
            if self.driver.url != self.initMy_url:
                sleep(1)
            else:
                break
    # 买票功能实现
    def start_buy(self):
        self.driver = Browser(driver_name=self.driver_name, executable_path=self.executable_path)
        #窗口大小的操作
        self.driver.driver.set_window_size(700, 500)
        self.login()
        self.driver.visit(self.ticket_url)
        try:
            print('开始购票...')
            # 加载查询信息
            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('查询').click()
                    count += 1
                    print('第%d次点击查询...' % count)
                    try:
                        self.driver.find_by_text('预订')[self.order-1].click()
                        sleep(1.5)
                    except Exception as e:
                        print(e)
                        print('预订失败...')
                        continue
            else:
                while self.driver.url == self.ticket_url:
                    self.driver.find_by_text('查询').click()
                    count += 1
                    print('第%d次点击查询...' % count)
                    try:
                        for i in self.driver.find_by_text('预订'):
                            i.click()
                            sleep(1)
                    except Exception as e:
                        print(e)
                        print('预订失败...')
                        continue
            print('开始预订...')
            sleep(1)
            print('开始选择用户...')
            for p in self.passengers:

                self.driver.find_by_text(p).last.click()
                sleep(0.5)
                if p[-1] == ')':
                    self.driver.find_by_id('dialog_xsertcj_ok').click()
            print('提交订单...')
            sleep(1)
            self.driver.find_by_text(self.YW).click()
            sleep(1)
            self.driver.find_by_id('submitOrder_id').click()
            sleep(2)
            print('确认选座...')
            self.driver.find_by_id('qr_submit_id').click()
            print('预订成功...')
        except Exception as e:
            print(e)


if __name__ == '__main__':
    # 用户名
    username = 'LVXIONG06'
    # 密码
    password = '******''
    # 车次选择,0代表所有车次
    order = 0
    # 乘客名,比如passengers = ['吕雄']
    # 学生票需注明,注明方式为:passengers = ['吕雄(学生)']
    passengers = ['吕雄(学生)']
    # 日期,格式为:'2018-03-10'
    dtime = '2018-03-10'
    # 出发地(需填写cookie值)
    starts = '%u5BA3%u5A01%2CXWM' #宣威
    # 目的地(需填写cookie值)
    ends = '%u5357%u660C%2CNCG' #北京

    Buy_Tickets(username, password, order, passengers, dtime, starts, ends).start_buy()
    


- WZ无座
- YZ硬座
- RZ软座
- YW硬卧
- SRRB动卧
- RW软卧
- GR高软
- ZE二等座
- ZY一等座
- TZ商务座
- %u5317%u4EAC%2CBJP北京
- %u5BA3%u5A01%2CXWM宣威
- %u6606%u660E%2CKMM昆明
- %u5357%u660C%2CNCG南昌
- value="1"成人票
- value="2"儿童票
- value="3"学生票
- value="4"残军票

##
- 火车票始终点cookie查询:
- console--javascript:alert(document.cookie)
##

原文链接:http://www.itongji.cn/cms/article/articledetails?articleid=6845


墨染半纸,清心煮字...