1. 程式人生 > >python搶火車票 簡訊通知

python搶火車票 簡訊通知

# -*- coding: utf-8 -*-
from splinter.browser import Browser
from time import sleep
import traceback
from twilio.rest import Client
# import sys
# reload(sys)
# sys.setdefaultencoding('utf-8')

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
        # self.xb = xb
        # self.pz = pz
        self.login_url = 'https://kyfw.12306.cn/otn/login/init'
        self.initMy_url = 'https://kyfw.12306.cn/otn/view/index.html'
        self.ticket_url = 'https://kyfw.12306.cn/otn/leftTicket/init'
        self.buy_url = 'https://kyfw.12306.cn/otn/confirmPassenger/initDc'
        self.driver_name = 'chrome'
        self.executable_path = 'E:\Python\Scripts\chromedriver.exe'
    def login(self):
        self.driver.visit(self.login_url)
        self.driver.fill('loginUserDTO.user_name', self.username)
        self.driver.fill('userDTO.password', self.passwd)
        print('輸入驗證碼...')
        while 1:
            if self.driver.url != self.initMy_url:
                sleep(5)
            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(1000, 700)
        self.login()
        self.driver.visit(self.ticket_url)
        try:
            self.check_ticket()
            self.buy_ticket()
        except Exception as e:
            print(e)
    def check_ticket(self):
        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('預訂'):
                        if i.has_class('btn72'):
                            i.click()
                            sleep(1)
                except Exception as e:
                    print(e)
                    print('預訂失敗...')
                    continue
                sleep(1)
    def buy_ticket(self):
        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.pz).click()
         # sleep(1)
        # self.driver.find_by_text(self.xb).click()
        # sleep(1)
        self.driver.find_by_id('submitOrder_id').click()
        sleep(2)
        print('確認選座...')
        self.driver.find_by_id('qr_submit_id').click()
        print('預訂成功...')
        sme_text = '搶到票了,趕緊去付款 !!\n I get a ticket,now going to pay !!'
        self.sendSMS(my_number='xxx',msg=sme_text)
        print '簡訊傳送成功'
    def sendSMS(self,my_number,msg='你好,這是來自你自己的手機測試資訊!'):
        account_sid = 'xxx'
        auth_token = 'xxx'
        twilio_number = 'xxx'
        client = Client(account_sid, auth_token)
        message = client.messages.create(from_=twilio_number,body=msg,to=my_number)
        print(message.sid)

if __name__ == '__main__':
    # 使用者名稱
    username = 'xxx'
    # 密碼
    password = 'xxx'
    # 車次選擇,0代表所有車次
    order = 0
    # 乘客名,學生票需註明,比如passengers = ['小紅', '小明(學生)']
    passengers = ['xxx']
    # 日期
    dtime = '2019-02-01'
    # 出發地(cookie值)
    starts = '%u5357%u4EAC%u5357%2CNKH' #南京南
    # 目的地(cookie值)
    ends = '%u6C49%u53E3%2CHKN' #漢口

    # xb =['硬座'] 
    # pz=['成人票']
    Buy_Tickets(username, password, order, passengers, dtime, starts, ends).start_buy()

 

下載 chromedriver.exe,映象網址  http://npm.taobao.org/mirrors/chromedriver/

傳送簡訊,申請token,   https://www.twilio.com/