Home  >  Article  >  Backend Development  >  Python implements 12306 train ticket query system

Python implements 12306 train ticket query system

高洛峰
高洛峰Original
2017-02-24 15:12:043310browse

Recently, I saw that python was used to implement train ticket query. I also implemented it myself. I felt that I gained a lot. I will share each step in detail below. (Note that python3 is used)

First I will show the final result:

Execute on the cmd command line: python tickets.py -dk shanghai chengdu 20161007 > result. txt

Python implements 12306 train ticket query system

means: Query the train information starting with D and K from Shanghai to Chengdu on October 7, 2016, and save it to the result.txt file; the following is result.txt The results in the file:

Python implements 12306 train ticket query system

The following will be the implementation steps:

1, Install third-party libraries pip install Installation: requests, docopt, prettytable

2. docopt can be used to parse parameters entered from the command line:

"""
Usage:
test [-gdtkz] <from> <to> <date>
Options:
-h,--help 显示帮助菜单
-g 高铁
-d 动车
-t 特快
-k 快速
-z 直达
Example:
tickets -gdt beijing shanghai 2016-08-25
"""
import docopt
args = docopt.docopt(__doc__)
print(args)
# 上面 """ """ 包含中的:
#Usage:
# test [-gdtkz] <from> <to> <date>
#是必须要的 test 是可以随便写的,不影响解析

The final printed result is a dictionary for later use:

Python implements 12306 train ticket query system

3. Obtain train information

Our interface for querying remaining votes in 12306:

url: 'https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version= 1.8968'

The method is: get

Transmitted parameters: queryDate:2016-10-05, from_station:CDW, to_station:SHH

The corresponding abbreviation of the city needs to be additional The interface query results in

3.1 Query the corresponding abbreviation of the city:

The url of this interface = 'https://kyfw.12306.cn/otn/resources/js/framework/station_name. js?station_version=1.8968'

The method is get, use regular expressions on the returned results, and extract the values ​​of the city name and abbreviation (the returned values ​​are similar: 7@cqn|Chongqing South|CRW|chongqingnan|cqn|, What we need is: CRW, chongqingnan), the code is as follows

parse_stations.py:

#coding=utf-8
from prettytable import PrettyTable
class TrainCollection(object):
"""
解析列车信息
"""
# 显示车次、出发/到达站、 出发/到达时间、历时、一等坐、二等坐、软卧、硬卧、硬座
header = &#39;序号 车次 出发站/到达站 出发时间/到达时间 历时 商务座 一等座 二等座 软卧 硬卧 硬座 无座&#39;.split()
def __init__(self,rows,traintypes):
self.rows = rows
self.traintypes = traintypes
def _get_duration(self,row):
"""
获取车次运行的时间
"""
duration = row.get(&#39;lishi&#39;).replace(&#39;:&#39;,&#39;小时&#39;) + &#39;分&#39;
if duration.startswith(&#39;00&#39;):
return duration[4:]
elif duration.startswith(&#39;0&#39;):
return duration[1:]
return duration
@property
def trains(self):
result = []
flag = 0
for row in self.rows:
if row[&#39;station_train_code&#39;][0] in self.traintypes:
flag += 1
train = [
# 序号
flag,
# 车次
row[&#39;station_train_code&#39;],
# 出发、到达站点
&#39;/&#39;.join([row[&#39;from_station_name&#39;],row[&#39;to_station_name&#39;]]),
# 成功、到达时间
&#39;/&#39;.join([row[&#39;start_time&#39;],row[&#39;arrive_time&#39;]]),
# duration 时间
self._get_duration(row),
# 商务座
row[&#39;swz_num&#39;],
# 一等座
row[&#39;zy_num&#39;],
# 二等座
row[&#39;ze_num&#39;],
# 软卧
row[&#39;rw_num&#39;],
# 硬卧
row[&#39;yw_num&#39;],
# 硬座
row[&#39;yz_num&#39;],
# 无座
row[&#39;wz_num&#39;]
]
result.append(train)
return result
def print_pretty(self):
"""打印列车信息"""
pt = PrettyTable()
pt._set_field_names(self.header)
for train in self.trains:
pt.add_row(train)
print(pt)
if __name__ == &#39;__main__&#39;:
t = TrainCollection()

The pprint module can be printed Information, easier to read:

Run in cmd: python parse_stations.py > stations.py

will get the stations.py file in the current directory. The file contains the site name and For short, add "stations = " to the stations.py file to create a dictionary to facilitate later values. Here is the content of the stations.py file:

Python implements 12306 train ticket query system

3.2 Now The parameters for obtaining train information have been prepared. The next step is to get the return value of the train and parse out the information you need, such as train number, first-class ticket number, etc. . , myprettytable.py

#coding=utf-8
from prettytable import PrettyTable
class TrainCollection(object):
"""
解析列车信息
"""
# 显示车次、出发/到达站、 出发/到达时间、历时、一等坐、二等坐、软卧、硬卧、硬座
header = &#39;序号 车次 出发站/到达站 出发时间/到达时间 历时 商务座 一等座 二等座 软卧 硬卧 硬座 无座&#39;.split()
def __init__(self,rows,traintypes):
self.rows = rows
self.traintypes = traintypes
def _get_duration(self,row):
"""
获取车次运行的时间
"""
duration = row.get(&#39;lishi&#39;).replace(&#39;:&#39;,&#39;小时&#39;) + &#39;分&#39;
if duration.startswith(&#39;00&#39;):
return duration[4:]
elif duration.startswith(&#39;0&#39;):
return duration[1:]
return duration
@property
def trains(self):
result = []
flag = 0
for row in self.rows:
if row[&#39;station_train_code&#39;][0] in self.traintypes:
flag += 1
train = [
# 序号
flag,
# 车次
row[&#39;station_train_code&#39;],
# 出发、到达站点
&#39;/&#39;.join([row[&#39;from_station_name&#39;],row[&#39;to_station_name&#39;]]),
# 成功、到达时间
&#39;/&#39;.join([row[&#39;start_time&#39;],row[&#39;arrive_time&#39;]]),
# duration 时间
self._get_duration(row),
# 商务座
row[&#39;swz_num&#39;],
# 一等座
row[&#39;zy_num&#39;],
# 二等座
row[&#39;ze_num&#39;],
# 软卧
row[&#39;rw_num&#39;],
# 硬卧
row[&#39;yw_num&#39;],
# 硬座
row[&#39;yz_num&#39;],
# 无座
row[&#39;wz_num&#39;]
]
result.append(train)
return result
def print_pretty(self):
"""打印列车信息"""
pt = PrettyTable()
pt._set_field_names(self.header)
for train in self.trains:
pt.add_row(train)
print(pt)
if __name__ == &#39;__main__&#39;:
t = TrainCollection()

prettytable This library can print out a format similar to that displayed by mysql query data,

4. The next step is to integrate the various modules: tickets.py

"""Train tickets query via command-line.
Usage:
tickets [-gdtkz] <from> <to> <date>
Options:
-h,--help 显示帮助菜单
-g 高铁
-d 动车
-t 特快
-k 快速
-z 直达
Example:
tickets -gdt beijing shanghai 2016-08-25
"""
import requests
from docopt import docopt
from stations import stations
# from pprint import pprint
from myprettytable import TrainCollection
class SelectTrain(object):
def __init__(self):
"""
获取命令行输入的参数
"""
self.args = docopt(__doc__)#这个是获取命令行的所有参数,返回的是一个字典
def cli(self):
"""command-line interface"""
# 获取 出发站点和目标站点
from_station = stations.get(self.args[&#39;<from>&#39;]) #出发站点
to_station = stations.get(self.args[&#39;<to>&#39;]) # 目的站点
leave_time = self._get_leave_time()# 出发时间
url = &#39;https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=ADULT&queryDate={0}&from_station={1}&to_station={2}&#39;.format(
leave_time,from_station,to_station)# 拼接请求列车信息的Url
# 获取列车查询结果
r = requests.get(url,verify=False)
traindatas = r.json()[&#39;data&#39;][&#39;datas&#39;] # 返回的结果,转化成json格式,取出datas,方便后面解析列车信息用
# 解析列车信息
traintypes = self._get_traintype()
views = TrainCollection(traindatas,traintypes)
views.print_pretty()
def _get_traintype(self):
"""
获取列车型号,这个函数的作用是的目的是:当你输入 -g 是只是返回 高铁,输入 -gd 返回动车和高铁,当不输参数时,返回所有的列车信息
""" 
traintypes = [&#39;-g&#39;,&#39;-d&#39;,&#39;-t&#39;,&#39;-k&#39;,&#39;-z&#39;]
# result = []
# for traintype in traintypes:
# if self.args[traintype]:
# result.append(traintype[-1].upper())
trains = [traintype[-1].upper() for traintype in traintypes if self.args[traintype]]
if trains:
return trains
else:
return [&#39;G&#39;,&#39;D&#39;,&#39;T&#39;,&#39;K&#39;,&#39;Z&#39;]
def _get_leave_time(self):
"""
获取出发时间,这个函数的作用是为了:时间可以输入两种格式:2016-10-05、20161005
"""
leave_time = self.args[&#39;<date>&#39;]
if len(leave_time) == 8:
return &#39;{0}-{1}-{2}&#39;.format(leave_time[:4],leave_time[4:6],leave_time[6:])
if &#39;-&#39; in leave_time:
return leave_time
if __name__ == &#39;__main__&#39;:
cli = SelectTrain()
cli.cli()

Okay, it’s basically over. According to the beginning, you can query I want the train information

The above is the Python script introduced by the editor to implement the 12306 train ticket query system. I hope it will be helpful to everyone. If you have any questions, please leave me a message. Editor Will reply to everyone promptly. I would also like to thank you all for your support of the PHP Chinese website!

For more articles related to the implementation of 12306 train ticket query system in Python, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn