使用python搶火車票

開發(fā)環(huán)境:python3.6 /splinter/Chrome驅(qū)動

tips:splinter是python的一個類庫,可以通過pip來安裝。這里主要記錄Chrome驅(qū)動的問題

首先查看你的Chrome瀏覽器的版本號

chromedriver版本支持的---Chrome版本

v2.34-------------------------------v61-63

v2.33----------------------------------v60-62

v2.32---------------------------------v59-61

v2.31---------------------------------v58-60

v2.30---------------------------------v58-60

v2.29---------------------------------v56-58

v2.28---------------------------------v55-57

v2.27--------------------------------v54-56

v2.26-----------------------------------v53-55

v2.25-----------------------------------v53-55

v2.24-------------------------------------v52-54

v2.23------------------------------------v51-53

chromedriver的鏈接在這里:http://chromedriver.storage.googleapis.com/index.html

下載對應(yīng)的驅(qū)動然后解壓把它放在python的安裝目錄下(與python.exe同一目錄)

然后就是可以寫代碼了


# -*- 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='' #驅(qū)動器的名稱,這里用我們的Chrome

executable_path=''? #驅(qū)動器的路徑

#用戶名,密碼

username = u"*************"

passwd = u"*****************"

# cookies自己去找, 下面兩個分別是蘇州, 徐州,利用Chrome的檢查工具中的NetWork

starts = u"%u82CF%u5DDE%2CSZH"

ends = u"%u5F90%u5DDE%2CXCH"

# 時(shí)間格式2018-01-19

dtime = u"2018-01-10"

# 車次,選擇第幾趟,0則從上之下依次點(diǎn)擊

order = 0

##乘客名

users = u"***********"

#席位

xb = u"二等座"

pz=u"成人票"

"""網(wǎng)址"""

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"

#login_url='https://kyfw.12306.cn/otn/login/init'

def __init__(self):

self.driver_name='chrome'

self.executable_path='C:\Program Files\Python\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)

print (u"等待驗(yàn)證碼,自行輸入...")

while True:

if self.driver.url != self.initmy_url:

sleep(1)

else:

print('登錄成功!')

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"購票頁面開始...")

# sleep(1)

# 加載查詢信息

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"循環(huán)點(diǎn)擊查詢... 第 %s 次" % count)

# sleep(1)

try:

self.driver.find_by_text(u"預(yù)訂")[self.order - 1].click()

except Exception as e:

print (e)

print (u"還沒開始預(yù)訂")

continue

else:

while self.driver.url == self.ticket_url:

self.driver.find_by_text(u"查詢").click()

count += 1

print (u"循環(huán)點(diǎn)擊查詢... 第 %s 次" % count)

# sleep(0.8)

try:

for i in self.driver.find_by_text(u"預(yù)訂"):

i.click()

sleep(1)

except Exception as e:

print (e)

print (u"還沒開始預(yù)訂 %s" %count)

continue

print (u"開始預(yù)訂...")

# sleep(3)

# self.driver.reload()

sleep(1)

print (u'開始選擇用戶...')

#for user in self.users:

?#self.driver.find_by_text(self.users)

self.driver.find_by_text(self.users)[0].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)

#當(dāng)買學(xué)生票的時(shí)候,會彈出一個溫馨提示

self.driver.find_by_id('dialog_xsertcj_ok').click()#確認(rèn)

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"確認(rèn)選座...")

self.driver.find_by_id('qr_submit_id').click()

self.driver.find_by_id('insurance_buy_and_agree')[0].click()

print('等待支付。。。。。')

except Exception as e:

print (e)

if __name__ == '__main__':

huoche=huoche()

huoche.start()

此代碼僅供參考,經(jīng)過我測試是可以運(yùn)行的

GitHub源碼:https://github.com/tianyu1030/python_huocheticket

有關(guān)splinter的方法可以參考官網(wǎng):http://splinter.readthedocs.io/en/latest/index.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容