Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test-pr #41

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 51 additions & 30 deletions hack12306.py
Original file line number Diff line number Diff line change
Expand Up @@ -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的绝对路径,默认为主程序当前目录
Expand All @@ -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")
# 车次
Expand Down Expand Up @@ -99,6 +107,7 @@ def loadConfig(self):
"""
加载映射文件,并将中文"武汉"转换成编码后的格式:“武汉,WHN“
"""

def loadCityCode(self):
print("映射出发地、目的地...")
city_codes = {}
Expand All @@ -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("开始登录...")
Expand All @@ -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()
Expand All @@ -185,6 +196,7 @@ def searchMore(self):
print(u"未指定发车时间,默认00:00-24:00")

"""填充查询条件"""

def preStart(self):
# 加载查询信息
# 出发地
Expand All @@ -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();
Expand All @@ -213,7 +225,7 @@ def specifyTrainNo(self):
continue

def buyOrderZero(self):
count=0
count = 0
while self.driver.url == self.ticket_url:
# 勾选车次类型,发车时间
self.searchMore();
Expand All @@ -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):
Expand All @@ -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"硬座余票<strong>0</strong>张") == None:
self.driver.find_by_id('qr_submit_id').click()
else:
Expand All @@ -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:
Expand Down Expand Up @@ -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)

Expand All @@ -313,6 +333,7 @@ def start(self):
# 自动购买车票
self.buyTickets();


if __name__ == '__main__':
print("===========hack12306 begin===========")
hackTickets = hackTickets()
Expand Down