汽車の切符の命令行の照会

2751 ワード

ダイレクトコード
#! usr/bin/env python3

# -*- coding:utf8 -*-

# author : DZG

#      python D:\Python\Coding\class201606\tickets\getTicketsPython.py changzhou suzhou 2016-09-17

"""Train tickets query via command-line.

Usage:

 tickets [-gdtkz]   

Options:

 -h,--help       

 -g   

 -d   

 -t   

 -k   

 -z   

Example:

 tickets beijing shanghai 2016-08-25

"""

import re,requests

from docopt import docopt

from prettytable import PrettyTable

class TrainCollection(object):

#     、  /   、   /    、  、   、   、  、  、  

header = '     /      /                        '.split()

def __init__(self, rows):

self.rows = rows

def _get_duration(self, row):

"""

         

 """

duration = row.get('lishi').replace(':', 'h') + 'm'

if duration.startswith('00'):

return duration[4:]

if duration.startswith('0'):

return duration[1:]

return duration

@property

def trains(self):

for row in self.rows:

train = [

#   

row['station_train_code'],

#   、   

'
'.join([row['from_station_name'], row['to_station_name']]), # 、 '
'.join([row['start_time'], row['arrive_time']]), # self._get_duration(row), # row['zy_num'], # row['ze_num'], # row['rw_num'], # row['yw_num'], # row['yz_num'] ] yield train def pretty_print(self): """ , 。 `prettytable` MySQL 。 """ pt = PrettyTable() # pt._set_field_names(self.header) for train in self.trains: pt.add_row(train) print(pt) def cli(): # arguments = docopt(__doc__) stations = getAddressDict() from_staion = stations.get(arguments['']) to_station = stations.get(arguments['']) date = arguments[''] # URL url = 'https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=ADULT&queryDate={}&from_station={}&to_station={}'.format( date, from_staion, to_station ) # verify=False r = requests.get(url, verify=False) rows = r.json()['data']['datas'] trains = TrainCollection(rows) trains.pretty_print() # def getAddressDict(): url = 'https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.8955' r = requests.get(url, verify=False) stations = re.findall(r'([A-Z]+)\|([a-z]+)', r.text) stations = dict(stations) stations = dict(zip(stations.values(), stations.keys())) return stations if __name__ == '__main__': cli()