国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

使用python腳本實(shí)現(xiàn)查詢火車(chē)票工具

2020-02-15 22:28:50
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

使用python腳本實(shí)現(xiàn)查詢火車(chē)票信息的效果圖如下:

實(shí)現(xiàn)的代碼:

# coding: utf-8"""命令行火車(chē)票查看器Usage: tickets [-gdtkz] Options: -h,--help 顯示幫助菜單 -g   高鐵 -d   動(dòng)車(chē) -t   特快 -k   快速 -z   直達(dá)Example: tickets 北京 上海 2016-10-10 tickets -dg 成都 南京 2016-10-10"""import jsonimport requestsimport prettytablefrom docopt import docoptfrom colorama import init, Foreclass CollectInfo: def __init__(self):  self.qurey_ret = []  self.header = ['車(chē)次信息', '發(fā)/到時(shí)間', '發(fā)/到站', '歷時(shí)', '票價(jià)', '余票'] # 獲取車(chē)次相關(guān)的所有信息 def query_html_ret(self, query_args):  url = 'http://api.12306.com/v1/train/trainInfos?arrStationCode={to_station}&deptDate={date}/    &deptStationCode={source_station}&findGD=false'.format(to_station=query_args['to_station'],                  source_station=query_args['source_station'],                  date=query_args['date'])  row_ret = requests.get(url)  return row_ret.json() # 解析獲取到的結(jié)果 def paser_ret(self, row_ret):  trains_info = row_ret['data']['trainInfos']  for info in trains_info:   row_info = []   # 獲取車(chē)次信息   row_info.append('/n' + info['trainCode'])   # 獲取車(chē)次到站時(shí)間信息   row_info.append('/n' + '/n'.join([Fore.GREEN + info['deptTime']+ Fore.RESET,            Fore.RED + info['arrTime']+ Fore.RESET]))   # 獲取車(chē)次站點(diǎn)名稱   row_info.append('/n' + '/n'.join([Fore.GREEN + info['deptStationName'] + Fore.RESET,            Fore.RED + info['arrStationName']+ Fore.RESET]))   # 獲取車(chē)次到達(dá)站點(diǎn)所需時(shí)間   row_info.append('/n' + info['runTime'])   # 獲取票價(jià)以及余票信息   seat_price = []   seat_num = []   for seat in info['seatList']:    seat_price.append(seat['seatName'] + ':' + seat['seatPrice'])    if int(seat['seatNum']) > 10:     ticknum = Fore.GREEN + seat['seatNum'] + Fore.RESET    else:     ticknum = seat['seatNum']    seat_num.append(ticknum)   row_info.append('/n'.join(seat_price))   row_info.append('/n'.join(seat_num))   self.qurey_ret.append(row_info)   self.qurey_ret.append([' ', ' ', ' ', ' ', ' ', ' '])  return self.qurey_ret def show_with_table(self):  ticket_table = prettytable.PrettyTable()  ticket_table.field_names = self.header  for row in self.qurey_ret:   if len(row) == 0:    continue   ticket_table.add_row(row)  return ticket_tabledef main(): arguments = docopt(__doc__) query_args = {} init() # 獲取所有站點(diǎn)信息(stations.txt信息通過(guò) 函數(shù)獲取) # https: // kyfw.12306.cn / otn / resources / js / framework / station_name.js?station_version = 1.8971 f = open('stations.txt', 'r') info = f.read() stations_info = json.loads(info) # 從所有站點(diǎn)信息中獲取所要查詢站點(diǎn)的代碼信息 query_args['to_station'] = stations_info[arguments['']] query_args['source_station'] = stations_info[arguments['']] query_args['date'] = arguments[''] # 向12306查詢,得到跟車(chē)次相關(guān)的所有信息 collect_train = CollectInfo() row_ret = collect_train.query_html_ret(query_args) collect_train.paser_ret(row_ret) table = collect_train.show_with_table() print(table) if __name__ == '__main__':  main()            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 兴城市| 玛曲县| 柳河县| 栾川县| 抚州市| 正镶白旗| 姜堰市| 班戈县| 北票市| 修武县| 门源| 通州市| 彭水| 平谷区| 西乡县| 视频| 木里| 修水县| 福贡县| 盘山县| 噶尔县| 茂名市| 武安市| 广州市| 尚志市| 新田县| 沭阳县| 铜陵市| 房山区| 林口县| 拜泉县| 宝清县| 阜城县| 惠水县| 安新县| 五家渠市| 中西区| 徐水县| 洪湖市| 无极县| 永安市|