diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml
new file mode 100644
index 0000000..ee613ac
--- /dev/null
+++ b/.idea/inspectionProfiles/Project_Default.xml
@@ -0,0 +1,30 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/hack12306.py b/hack12306.py
index 9b2fd2a..7162f31 100644
--- a/hack12306.py
+++ b/hack12306.py
@@ -16,20 +16,24 @@
python hack12306.py
"""
-from splinter.browser import Browser
-from configparser import ConfigParser
-from time import sleep
-import traceback
-import time, sys
-import codecs
import argparse
+import codecs
import os
+import sys
+import time
import time
+import traceback
+from configparser import ConfigParser
+from time import sleep
+
+from splinter.browser import Browser
+
class hackTickets(object):
"""docstring for hackTickets"""
"""读取配置文件"""
+
def readConfig(self, config_file='config.ini'):
print("加载配置文件...")
# 补充文件路径,获得config.ini的绝对路径,默认为主程序当前目录
@@ -50,10 +54,14 @@ def readConfig(self, config_file='config.ini'):
# 始发站
starts_city = cp.get("cookieInfo", "starts")
# config.ini配置的是中文,转换成"武汉,WHN",再进行编码
- self.starts = self.convertCityToCode(starts_city).encode('unicode_escape').decode("utf-8").replace("\\u", "%u").replace(",", "%2c")
+ self.starts = self.convertCityToCode(starts_city).encode('unicode_escape').decode("utf-8").replace("\\u",
+ "%u").replace(
+ ",", "%2c")
# 终点站
ends_city = cp.get("cookieInfo", "ends");
- self.ends = self.convertCityToCode(ends_city).encode('unicode_escape').decode("utf-8").replace("\\u", "%u").replace(",", "%2c")
+ self.ends = self.convertCityToCode(ends_city).encode('unicode_escape').decode("utf-8").replace("\\u",
+ "%u").replace(
+ ",", "%2c")
# 乘车时间
self.dtime = cp.get("cookieInfo", "dtime")
# 车次
@@ -99,6 +107,7 @@ def loadConfig(self):
"""
加载映射文件,并将中文"武汉"转换成编码后的格式:“武汉,WHN“
"""
+
def loadCityCode(self):
print("映射出发地、目的地...")
city_codes = {}
@@ -118,29 +127,30 @@ def convertCityToCode(self, c):
return False
"""加载席别编码"""
+
def loadSeatType(self):
self.seatMap = {
- "硬座" : "1",
- "硬卧" : "3",
- "软卧" : "4",
- "一等软座" : "7",
- "二等软座" : "8",
- "商务座" : "9",
- "一等座" : "M",
- "二等座" : "O",
- "混编硬座" : "B",
- "特等座" : "P"
+ "硬座": "1",
+ "硬卧": "3",
+ "软卧": "4",
+ "一等软座": "7",
+ "二等软座": "8",
+ "商务座": "9",
+ "一等座": "M",
+ "二等座": "O",
+ "混编硬座": "B",
+ "特等座": "P"
}
def __init__(self):
# 读取城市中文与三字码映射文件,获得转换后到城市信息-- “武汉”: "武汉,WHN"
- self.city_codes = self.loadCityCode();
+ self.city_codes = self.loadCityCode()
# 加载席别
self.loadSeatType()
# 读取配置文件,获得初始化参数
- self.loadConfig();
+ self.loadConfig()
def login(self):
print("开始登录...")
@@ -161,16 +171,17 @@ def login(self):
break
"""更多查询条件"""
+
def searchMore(self):
# 选择车次类型
for type in self.train_types:
# 车次类型选择
- train_type_dict = {'T': u'T-特快', # 特快
- 'G': u'GC-高铁/城际', # 高铁
- 'D': u'D-动车', # 动车
- 'Z': u'Z-直达', # 直达
- 'K': u'K-快速' # 快速
- }
+ train_type_dict = {'T': u'T-特快', # 特快
+ 'G': u'GC-高铁/城际', # 高铁
+ 'D': u'D-动车', # 动车
+ 'Z': u'Z-直达', # 直达
+ 'K': u'K-快速' # 快速
+ }
if type == 'T' or type == 'G' or type == 'D' or type == 'Z' or type == 'K':
print(u'--------->选择的车次类型', train_type_dict[type])
self.driver.find_by_text(train_type_dict[type]).click()
@@ -185,6 +196,7 @@ def searchMore(self):
print(u"未指定发车时间,默认00:00-24:00")
"""填充查询条件"""
+
def preStart(self):
# 加载查询信息
# 出发地
@@ -195,7 +207,7 @@ def preStart(self):
self.driver.cookies.add({"_jc_save_fromDate": self.dtime})
def specifyTrainNo(self):
- count=0
+ count = 0
while self.driver.url == self.ticket_url:
# 勾选车次类型,发车时间
self.searchMore();
@@ -213,7 +225,7 @@ def specifyTrainNo(self):
continue
def buyOrderZero(self):
- count=0
+ count = 0
while self.driver.url == self.ticket_url:
# 勾选车次类型,发车时间
self.searchMore();
@@ -230,7 +242,7 @@ def buyOrderZero(self):
except Exception as e:
print(e)
- print(u"还没开始预订 %s" %count)
+ print(u"还没开始预订 %s" % count)
continue
def selUser(self):
@@ -254,6 +266,8 @@ def confirmSeat(self):
# 若提交订单异常,请适当加大sleep的时间
sleep(1)
print(u"确认选座...")
+
+ self.driver.find_by_id('qr_submit_id').click()
if self.driver.find_by_text(u"硬座余票0张") == None:
self.driver.find_by_id('qr_submit_id').click()
else:
@@ -262,6 +276,7 @@ def confirmSeat(self):
elif self.noseat_allow == 1:
self.driver.find_by_id('qr_submit_id').click()
+
def buyTickets(self):
t = time.clock()
try:
@@ -290,17 +305,22 @@ def buyTickets(self):
# 提交订单
self.submitOrder()
# 确认选座
+
+ # self.confirmSeat()
+
self.confirmSeat()
+
print(time.clock() - t)
except Exception as e:
print(e)
"""入口函数"""
+
def start(self):
# 初始化驱动
- self.driver=Browser(driver_name=self.driver_name,executable_path=self.executable_path)
+ self.driver = Browser(driver_name=self.driver_name, executable_path=self.executable_path)
# 初始化浏览器窗口大小
self.driver.driver.set_window_size(1400, 1000)
@@ -313,6 +333,7 @@ def start(self):
# 自动购买车票
self.buyTickets();
+
if __name__ == '__main__':
print("===========hack12306 begin===========")
hackTickets = hackTickets()