Recently I saw the use of Python to realize train ticket query, I also realized, I feel a lot of harvest, the following I will each step in detail to share. (Note that the Python3 is used)
First I show the final result:
Execute at cmd command line: Python tickets.py-dk Shanghai Chengdu 20161007 > Result.txt
Check the train information of Shanghai-Chengdu 2016.10.07 D and K, and save to Result.txt file; Here is the result in the Result.txt file:
The following will be the implementation steps:
1. Install third-party library PIP install: 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 Menu-G high-speed train-t Express-K fast-Z Direct example: TICKETS-GDT Beijing Shanghai 2016-08-25 "" "" Import Docoptargs = docopt.docopt (__doc__) print (args) # above "" "" Contains: #Usage: # test [-gdtkz] <from> <to> <date> #是必须要的 test can be freely written, without affecting the parsing
The result of the final printing is a dictionary that is convenient for later use:
3. Access to train information
We check the interface in 12306 for the remainder of the ticket:
URL: ' https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.8968 '
Method is: Get
Parameters for transmission: querydate:2016-10-05, FROM_STATION:CDW, To_station:shh
Where the city correspondence abbreviation is required additional interface query to derive
3.1 Find the abbreviation for City correspondence:
The URL of this interface = ' https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.8968 '
The method is get, with the return result using regular expressions, take out the city name and the value of the abbreviation (the returned value is similar: 7@cqn| Chongqing South | crw|chongqingnan|cqn|, what we need is: CRW, Chongqingnan), the code is as follows
parse_stations.py:
#coding =utf-8from prettytable Import prettytableclass traincollection (object): "" "" "" "" "" # shows the train, departure/arrival station, departure/arrival time, duration, Sit, second-class sit, soft sleeper, hard sleeper, hard seat header = ' Serial number train departure station/arrival station departure time/arrival time elapsed business Seat class two-seater soft sleeper hard sleeper seat ". Split () def __init__ (self,rows,traintypes): SE Lf.rows = Rowsself.traintypes = Traintypesdef _get_duration (self,row): "" Gets the time that the train Runs "" "duration = Row.get (' Lishi '). Replace (': ', ' hour ') + ' if Duration.startswith ' (' xx '): Return duration[4:]elif Duration.startswith (' 0 '): return duration [1:]return Duration@propertydef trains (self): result = []flag = 0for row in self.rows:if row[' Station_train_code '][0] in SE Lf.traintypes:flag + = 1train = [# serial number flag,# train row[' Station_train_code '],# departure, arrival site '/'. Join ([row[' From_station_name '], row[' To_station_name ']), # Success, arrival time '/'. Join ([row[' start_time '],row[' Arrive_time ']]), # Duration Time self._get_ Duration (row), # Business seat row[' swz_num '],# row[' Zy_num '],# second class row[' Ze_num '],# soft sleeper row[' rw_num '],# hard sleeper row[' yw_num '],# hard seat row[ ' Yz_num '],# no seat row[' Wz_num ']]result.append (train) return Resultdef Print_preTTY (self): "" "Print Train Info" "" pt = prettytable () pt._set_field_names (Self.header) for train in Self.trains:pt.add_row (train) Print (PT) If __name__ = = ' __main__ ': t = traincollection ()
Which pprint this module can be printed out of the information, more convenient to read:
Run in cmd: Python parse_stations.py > stations.py
will be in the current directory to get the stations.py file, the file is the site name and abbreviation, in the stations.py file added "stations =" This is a dictionary, convenient to the back of the value, the following is the contents of the stations.py file:
3.2 Now get the train information parameters are ready to be, the next is to get the return value of the train, to parse out the information they need, such as: Train number, the number of votes in the seats and so on. , myprettytable.py
#coding =utf-8from prettytable Import prettytableclass traincollection (object): "" "" "" "" "" # shows the train, departure/arrival station, departure/arrival time, duration, Sit, second-class sit, soft sleeper, hard sleeper, hard seat header = ' Serial number train departure station/arrival station departure time/arrival time elapsed business Seat class two-seater soft sleeper hard sleeper seat ". Split () def __init__ (self,rows,traintypes): SE Lf.rows = Rowsself.traintypes = Traintypesdef _get_duration (self,row): "" Gets the time that the train Runs "" "duration = Row.get (' Lishi '). Replace (': ', ' hour ') + ' if Duration.startswith ' (' xx '): Return duration[4:]elif Duration.startswith (' 0 '): return duration [1:]return Duration@propertydef trains (self): result = []flag = 0for row in self.rows:if row[' Station_train_code '][0] in SE Lf.traintypes:flag + = 1train = [# serial number flag,# train row[' Station_train_code '],# departure, arrival site '/'. Join ([row[' From_station_name '], row[' To_station_name ']), # Success, arrival time '/'. Join ([row[' start_time '],row[' Arrive_time ']]), # Duration Time self._get_ Duration (row), # Business seat row[' swz_num '],# row[' Zy_num '],# second class row[' Ze_num '],# soft sleeper row[' rw_num '],# hard sleeper row[' yw_num '],# hard seat row[ ' Yz_num '],# no seat row[' Wz_num ']]result.append (train) return Resultdef Print_preTTY (self): "" "Print Train Info" "" pt = prettytable () pt._set_field_names (Self.header) for train in Self.trains:pt.add_row (train) Print (PT) If __name__ = = ' __main__ ': t = traincollection ()
Prettytable This library is able to print out a format similar to the MySQL query data display,
4. The next step is to integrate each module: tickets.py
"" "Train tickets query via command-line. usage:tickets [-gdtkz] <from> <to> <date>options:-h,--Help Menu-G HSR-D train-t Express-K fast-Z Direct example: TICKETS-GDT Beijing Shanghai 2016-08-25 "" "Import requestsfrom docopt import docoptfrom stations import stations# from PPR int import pprintfrom myprettytable import Traincollectionclass selecttrain (object):d EF __init__ (self): "" gets arguments for command line input " "" Self.args = docopt (__doc__) #这个是获取命令行的所有参数, returns a dictionary def CLI (self): "" "Command-Line Interface" "" # Get the departure site and destination site From_ Station = Stations.get (self.args[' <from> ') #出发站点to_station = Stations.get (self.args[' <to> ')) # destination site leave _time = Self._get_leave_time () # departure Time URL = ' Https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=ADULT&queryDate ={0}&FROM_STATION={1}&TO_STATION={2} '. Format (leave_time,from_station,to_station) # Stitching request train Information url# Get train Query Results r = Requests.get (url,verify=false) Traindatas = R.json () [' Data '] [' datas '] # return results, convert to JSON format, remove Datas, Easy to parse train information with # Parse train information traintypes = Self._get_traintyPE () views = Traincollection (traindatas,traintypes) views.print_pretty () def _get_traintype (self): "" "Get Train model, The purpose of this function is: when you enter-G is just return the high-speed rail, enter-GD back to the bullet train and high-speed rail, when not losing parameters, return all trains Information "" "Traintypes = ['-G ', '-d ', '-t ', '-K ', '-Z ']# 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 trainselse:return [' G ', ' D ', ' T ', ' K ', ' Z ']def _get_ Leave_time (self): "" "gets the departure time, the function is to: time can be entered in two formats: 2016-10-05, 20161005" "" Leave_time = self.args[' <date> ']if Len (leave_time) = = 8:return ' {0}-{1}-{2} '. Format (leave_time[:4],leave_time[4:6],leave_time[6:]) If '-' in Leave_time: return Leave_timeif __name__ = = ' __main__ ': CLI = Selecttrain () cli.cli ()
Well, basically it's over, and at the beginning of that, you'll be able to find the information you want.
The above is a small part of the Python script to introduce the implementation of the 12306 train ticket query system, I hope we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for your support for topic.alibabacloud.com!