From 1d5a74c4e85094264cc2f0e1e9f2a41bf27d3482 Mon Sep 17 00:00:00 2001
From: Quincy <291079050@qq.com>
Date: Mon, 14 May 2018 16:36:49 +0800
Subject: [PATCH 1/3] format code
---
hack12306.py | 104 +++++++++++++++++++++++++++++----------------------
1 file changed, 59 insertions(+), 45 deletions(-)
diff --git a/hack12306.py b/hack12306.py
index 9f31baa..3096539 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")
# 车次
@@ -95,6 +103,7 @@ def loadConfig(self):
"""
加载映射文件,并将中文"武汉"转换成编码后的格式:“武汉,WHN“
"""
+
def loadCityCode(self):
print("映射出发地、目的地...")
city_codes = {}
@@ -114,18 +123,19 @@ 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):
@@ -155,32 +165,34 @@ def login(self):
sleep(1)
else:
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()
else:
print(u"车次类型异常或未选择!(train_type=%s)" % type)
-
+
# 选择发车时间
print(u'--------->选择的发车时间', self.start_time)
if self.start_time:
self.driver.find_option_by_text(self.start_time).first.click()
else:
print(u"未指定发车时间,默认00:00-24:00")
-
+
"""填充查询条件"""
+
def preStart(self):
# 加载查询信息
# 出发地
@@ -191,7 +203,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();
@@ -199,7 +211,7 @@ def specifyTrainNo(self):
self.driver.find_by_text(u"查询").click()
count += 1
print(u"循环点击查询... 第 %s 次" % count)
-
+
try:
self.driver.find_by_text(u"预订")[self.order - 1].click()
sleep(0.3)
@@ -207,9 +219,9 @@ def specifyTrainNo(self):
print(e)
print(u"还没开始预订")
continue
-
+
def buyOrderZero(self):
- count=0
+ count = 0
while self.driver.url == self.ticket_url:
# 勾选车次类型,发车时间
self.searchMore();
@@ -217,7 +229,7 @@ def buyOrderZero(self):
self.driver.find_by_text(u"查询").click()
count += 1
print(u"循环点击查询... 第 %s 次" % count)
-
+
try:
for i in self.driver.find_by_text(u"预订"):
i.click()
@@ -226,41 +238,41 @@ def buyOrderZero(self):
except Exception as e:
print(e)
- print(u"还没开始预订 %s" %count)
+ print(u"还没开始预订 %s" % count)
continue
-
+
def selUser(self):
print(u'开始选择用户...')
for user in self.users:
self.driver.find_by_text(user).last.click()
-
+
def confirmOrder(self):
print(u"选择席别...")
if self.seatType:
self.driver.find_by_value(self.seatType).click()
else:
print(u"未指定席别,按照12306默认席别")
-
+
def submitOrder(self):
print(u"提交订单...")
sleep(1)
self.driver.find_by_id('submitOrder_id').click()
-
+
def confirmSeat(self):
# 若提交订单异常,请适当加大sleep的时间
sleep(1)
print(u"确认选座...")
self.driver.find_by_id('qr_submit_id').click()
-
+
def buyTickets(self):
t = time.clock()
try:
print(u"购票页面开始...")
-
+
# 填充查询条件
self.preStart()
-
+
# 带着查询条件,重新加载页面
self.driver.reload()
@@ -272,7 +284,7 @@ def buyTickets(self):
# 默认选票
self.buyOrderZero()
print(u"开始预订...")
-
+
sleep(0.8)
# 选择用户
self.selUser()
@@ -281,7 +293,7 @@ def buyTickets(self):
# 提交订单
self.submitOrder()
# 确认选座
- #self.confirmSeat()
+ # self.confirmSeat()
print(time.clock() - t)
@@ -289,9 +301,10 @@ def buyTickets(self):
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)
@@ -304,6 +317,7 @@ def start(self):
# 自动购买车票
self.buyTickets();
+
if __name__ == '__main__':
print("===========hack12306 begin===========")
hackTickets = hackTickets()
From b23745f366c72cc2fe40adec84b3ed141676e5be Mon Sep 17 00:00:00 2001
From: Quincy <291079050@qq.com>
Date: Mon, 14 May 2018 16:41:19 +0800
Subject: [PATCH 2/3] format code
---
.idea/inspectionProfiles/Project_Default.xml | 30 ++++++++++++++++++++
.idea/vcs.xml | 6 ++++
2 files changed, 36 insertions(+)
create mode 100644 .idea/inspectionProfiles/Project_Default.xml
create mode 100644 .idea/vcs.xml
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
From 78e53165d1e0388fbf21d49e81d73e97e372c84c Mon Sep 17 00:00:00 2001
From: Quincy <291079050@qq.com>
Date: Mon, 14 May 2018 16:47:21 +0800
Subject: [PATCH 3/3] test-pr
---
hack12306.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/hack12306.py b/hack12306.py
index 3096539..dce8a3c 100644
--- a/hack12306.py
+++ b/hack12306.py
@@ -140,13 +140,13 @@ def loadSeatType(self):
def __init__(self):
# 读取城市中文与三字码映射文件,获得转换后到城市信息-- “武汉”: "武汉,WHN"
- self.city_codes = self.loadCityCode();
+ self.city_codes = self.loadCityCode()
# 加载席别
self.loadSeatType()
# 读取配置文件,获得初始化参数
- self.loadConfig();
+ self.loadConfig()
def login(self):
print("开始登录...")