Skip to content

Commit 8040d02

Browse files
committed
用Python抢火车票的简单小程序
1 parent 4961413 commit 8040d02

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

12306.py

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author: liuyw
4+
"""
5+
from splinter.browser import Browser
6+
from time import sleep
7+
import traceback
8+
import time, sys
9+
10+
class huoche(object):
11+
driver_name = ''
12+
executable_path = ''
13+
#用户名,密码
14+
username = u"xxx"
15+
passwd = u"xxx"
16+
# cookies值得自己去找, 下面两个分别是沈阳, 哈尔滨
17+
starts = u"%u6C88%u9633%2CSYT"
18+
ends = u"%u54C8%u5C14%u6EE8%2CHBB"
19+
20+
# 时间格式2018-01-19
21+
dtime = u"2018-01-19"
22+
# 车次,选择第几趟,0则从上之下依次点击
23+
order = 0
24+
###乘客名
25+
users = [u"xxx",u"xxx"]
26+
##席位
27+
xb = u"二等座"
28+
pz = u"成人票"
29+
30+
"""网址"""
31+
ticket_url = "https://kyfw.12306.cn/otn/leftTicket/init"
32+
login_url = "https://kyfw.12306.cn/otn/login/init"
33+
initmy_url = "https://kyfw.12306.cn/otn/index/initMy12306"
34+
buy = "https://kyfw.12306.cn/otn/confirmPassenger/initDc"
35+
36+
def __init__(self):
37+
self.driver_name = 'chrome'
38+
self.executable_path = 'D:/chromedriver'
39+
40+
def login(self):
41+
self.driver.visit(self.login_url)
42+
self.driver.fill("loginUserDTO.user_name", self.username)
43+
# sleep(1)
44+
self.driver.fill("userDTO.password", self.passwd)
45+
print(u"等待验证码,自行输入...")
46+
while True:
47+
if self.driver.url != self.initmy_url:
48+
sleep(1)
49+
else:
50+
break
51+
52+
def start(self):
53+
self.driver = Browser(driver_name=self.driver_name,executable_path=self.executable_path)
54+
self.driver.driver.set_window_size(1400, 1000)
55+
self.login()
56+
# sleep(1)
57+
self.driver.visit(self.ticket_url)
58+
try:
59+
print(u"购票页面开始...")
60+
# sleep(1)
61+
# 加载查询信息
62+
self.driver.cookies.add({"_jc_save_fromStation": self.starts})
63+
self.driver.cookies.add({"_jc_save_toStation": self.ends})
64+
self.driver.cookies.add({"_jc_save_fromDate": self.dtime})
65+
66+
self.driver.reload()
67+
68+
count = 0
69+
if self.order != 0:
70+
while self.driver.url == self.ticket_url:
71+
self.driver.find_by_text(u"查询").click()
72+
count += 1
73+
print(u"循环点击查询... 第 %s 次" % count)
74+
# sleep(1)
75+
try:
76+
self.driver.find_by_text(u"预订")[self.order - 1].click()
77+
except Exception as e:
78+
print(e)
79+
print(u"还没开始预订")
80+
continue
81+
else:
82+
while self.driver.url == self.ticket_url:
83+
self.driver.find_by_text(u"查询").click()
84+
count += 1
85+
print(u"循环点击查询... 第 %s 次" % count)
86+
# sleep(0.8)
87+
try:
88+
for i in self.driver.find_by_text(u"预订"):
89+
i.click()
90+
sleep(1)
91+
except Exception as e:
92+
print(e)
93+
print(u"还没开始预订 %s" % count)
94+
continue
95+
print(u"开始预订...")
96+
# sleep(3)
97+
# self.driver.reload()
98+
sleep(1)
99+
print(u'开始选择用户...')
100+
for user in self.users:
101+
self.driver.find_by_text(user).last.click()
102+
103+
print(u"提交订单...")
104+
sleep(1)
105+
self.driver.find_by_text(self.pz).click()
106+
self.driver.find_by_id('').select(self.pz)
107+
# sleep(1)
108+
self.driver.find_by_text(self.xb).click()
109+
sleep(1)
110+
self.driver.find_by_id('submitOrder_id').click()
111+
print(u"开始选座...")
112+
self.driver.find_by_id('1D').last.click()
113+
self.driver.find_by_id('1F').last.click()
114+
115+
sleep(1.5)
116+
print(u"确认选座...")
117+
self.driver.find_by_id('qr_submit_id').click()
118+
119+
except Exception as e:
120+
print(e)
121+
122+
if __name__ == '__main__':
123+
huoche = huoche()
124+
huoche.start()

0 commit comments

Comments
 (0)