日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

python爬蟲學習之用Python搶火車票的簡單小程序

 

前言

Python現在非常火,語法簡單而且功能強大,很多同學都想學Python!所以小的給各位看官們準備了高價值Python學習視頻教程及相關電子版書籍,歡迎前來領取!

利用Python制作自動搶火車票小程序,過年再也不要擔心沒票了!

前言

每次過年很多人都會因為搶不到火車票而回不了家,所以小編利用Python寫了一個自動搶火車票的工具,希望大家能搶到火車票,回家過個好年!

 

python爬蟲學習之用Python搶火車票的簡單小程序

 

 

話不多說,直接上代碼:

  •  
@@ -0,0 +1,124 @@
# -*- coding: utf-8 -*-
"""
@author: liuyw
"""
from splinter.browser import Browser
from time import sleep
import traceback
import time, sys
class huoche(object):
 driver_name = ''
 executable_path = ''
 #用戶名,密碼
 username = u"xxx"
 passwd = u"xxx"
 # cookies值得自己去找, 下面兩個分別是沈陽, 哈爾濱
 starts = u"%u6C88%u9633%2CSYT"
 ends = u"%u54C8%u5C14%u6EE8%2CHBB"
 # 時間格式2018-01-19
 dtime = u"2018-01-19"
 # 車次,選擇第幾趟,0則從上之下依次點擊
 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 = 'D:/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"購票頁面開始...")
 # 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"循環點擊查詢... 第 %s 次" % 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"循環點擊查詢... 第 %s 次" % 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()

分享到:
標簽:火車票
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定