使用python腳本實(shí)現(xiàn)查詢火車(chē)票工具
使用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 json
import requests
import prettytable
from docopt import docopt
from colorama import init, Fore
class 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)名稱(chēng)
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_table
def 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()
總結(jié)
以上所述是小編給大家介紹的使用python腳本查詢火車(chē)票工具,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Python腳本實(shí)現(xiàn)12306火車(chē)票查詢系統(tǒng)
- Python實(shí)現(xiàn)12306火車(chē)票搶票系統(tǒng)
- python實(shí)現(xiàn)12306火車(chē)票查詢器
- 利用Python實(shí)現(xiàn)命令行版的火車(chē)票查看器
- 基于Python3.6+splinter實(shí)現(xiàn)自動(dòng)搶火車(chē)票
- 基于Python實(shí)現(xiàn)快遞信息提取
- python實(shí)現(xiàn)快遞價(jià)格查詢系統(tǒng)
- Python編寫(xiě)車(chē)票訂購(gòu)系統(tǒng)?Python實(shí)現(xiàn)快遞收費(fèi)系統(tǒng)
相關(guān)文章
numpy 中l(wèi)inspace函數(shù)的使用
本文主要介紹了numpy 中l(wèi)inspace函數(shù)的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
Python操作csv文件之csv.writer()和csv.DictWriter()方法的基本使用
csv文件是一種逗號(hào)分隔的純文本形式存儲(chǔ)的表格數(shù)據(jù),Python內(nèi)置了CSV模塊,可直接通過(guò)該模塊實(shí)現(xiàn)csv文件的讀寫(xiě)操作,下面這篇文章主要給大家介紹了關(guān)于Python操作csv文件之csv.writer()和csv.DictWriter()方法的基本使用,需要的朋友可以參考下2022-09-09
python繪制超炫酷動(dòng)態(tài)Julia集示例
大家好,本篇文章主要講的是python繪制超炫酷動(dòng)態(tài)Julia集示例,感興趣的痛學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
PyCharm安裝庫(kù)numpy失敗問(wèn)題的詳細(xì)解決方法
今天使用pycharm編譯python程序時(shí),由于要調(diào)用numpy包,但又未曾安裝numpy,于是就根據(jù)pycharm的提示進(jìn)行安裝,最后竟然提示出錯(cuò),下面這篇文章主要給大家介紹了關(guān)于PyCharm安裝庫(kù)numpy失敗問(wèn)題的詳細(xì)解決方法,需要的朋友可以參考下2022-06-06
python-itchat 統(tǒng)計(jì)微信群、好友數(shù)量,及原始消息數(shù)據(jù)的實(shí)例
今天小編就為大家分享一篇python-itchat 統(tǒng)計(jì)微信群、好友數(shù)量,及原始消息數(shù)據(jù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
django-crontab實(shí)現(xiàn)服務(wù)端的定時(shí)任務(wù)的示例代碼
這篇文章主要介紹了django-crontab實(shí)現(xiàn)服務(wù)端的定時(shí)任務(wù)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

