在win10上利用Python3.6制作火车票余票查询小程序

xiaoxiao2021-02-28  97

import re import requests from pprint import pprint url = 'https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.8971' response = requests.get(url, verify=False) stations = re.findall(u'([\u4e00-\u9fa5]+)\|([A-Z]+)', response.text) pprint(dict(stations), indent=4) 将上面代码运行产生的输出保存为stations.py,并将字典赋给变量stations用pip安装没有的模块,具体的一些程序的作用可以参考实验楼中关于火车票余票查询的课程from stations import stations from prettytable import PrettyTable#格式化信息打印工具 import requests import urllib3 import copy from colorama import init, Fore#命令行着色工具 init()"""取键,返回中文车站名"""def _get_keys(station_eg): for key in stations: if stations[key] == station_eg: return key class TrainsCollection: header = '车次 车站 时间 历时 商务座 一等座 二等座 高级软卧 软卧 动卧 硬卧 软座 硬座 无座 其他'.split() def __init__(self, available_trains): """ :param available_trains: 官网提取到的json数据列表 """ self.available_trains = available_trains """切割""" def _cut_train(self): saperated_info = [] for i in range(len(self.available_trains)): saperated_info.append((self.available_trains[i]).split('|')) #print(saperated_info[0]) """构建字典列表""" dict_header = ['train_number', 'from_station', 'to_station', 'from_time', 'to_time', 'spend_time', 'business_class', 'first_class', 'second_class', 'Advanced_soft_sleeper', 'soft_sleeper', 'PowerCar_sleeper', 'hard_sleeper', 'soft_seat', 'hard_seat', 'none_seat', 'other'] refer_to_num = [ 3, 6, 7, 8, 9, 10, 32, 31, 30, 21, 23, 33, 28, 24, 29, 26, 22] train_list = [] train_dict = {} for times in range(len(saperated_info)): for key in range(len(dict_header)): if saperated_info[times][refer_to_num[key]]: train_dict[dict_header[key]] = saperated_info[times][refer_to_num[key]] else: train_dict[dict_header[key]] = '--' train_list.append(copy.deepcopy(train_dict)) return train_list @property def trains(self): for raw_train in self._cut_train(): train_no = raw_train['train_number'] initial = train_no[0].lower() train = [ train_no, '\n'.join([Fore.GREEN + _get_keys(raw_train['from_station']) + Fore.RESET, Fore.RED + _get_keys(raw_train['to_station']) + Fore.RESET]), '\n'.join([Fore.GREEN + raw_train['from_time'] + Fore.RESET, Fore.RED + raw_train['to_time'] + Fore.RESET]), raw_train['spend_time'], raw_train['business_class'], raw_train['first_class'], raw_train['second_class'], raw_train['Advanced_soft_sleeper'], raw_train['soft_sleeper'], raw_train['PowerCar_sleeper'], raw_train['hard_sleeper'], raw_train['soft_seat'], raw_train['hard_seat'], raw_train['none_seat'], raw_train['other'], ] yield train def pretty_print(self): pt = PrettyTable() pt._set_field_names(self.header) for train in self.trains: pt.add_row(train) print(pt) def cli(): StartStation = input('出发地:') EndStation = input('目的地:') Data = input('出发日期:') from_station = stations[StartStation] to_station = stations[EndStation] date = Data url = 'https://kyfw.12306.cn/otn/leftTicket/query?'\ 'leftTicketDTO.train_date={}&leftTicketDTO.from_station'\ '={}&leftTicketDTO.to_station={}&purpose_codes=ADULT'.format( date, from_station, to_station) urllib3.disable_warnings() r = requests.get(url, verify=False) available_trains = r.json()['data']['result'] TrainsCollection(available_trains).pretty_print() if __name__ == '__main__': print('Example:\r') print('出发地:南京\n目的地:北京\n出发日期:2017-07-07\n') while(True): cli() 效果如下图所示:
转载请注明原文地址: https://www.6miu.com/read-40630.html

最新回复(0)