使用python脚本实现查询火车票工具
作者:白天不懂夜的黑i 时间:2021-03-10 05:48:54
使用python脚本实现查询火车票信息的效果图如下:
实现的代码:
# coding: utf-8
"""命令行火车票查看器
Usage:
tickets [-gdtkz]
Options:
-h,--help 显示帮助菜单
-g 高铁
-d 动车
-t 特快
-k 快速
-z 直达
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 = ['车次信息', '发/到时间', '发/到站', '历时', '票价', '余票']
# 获取车次相关的所有信息
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()
# 解析获取到的结果
def paser_ret(self, row_ret):
trains_info = row_ret['data']['trainInfos']
for info in trains_info:
row_info = []
# 获取车次信息
row_info.append('\n' + info['trainCode'])
# 获取车次到站时间信息
row_info.append('\n' + '\n'.join([Fore.GREEN + info['deptTime']+ Fore.RESET,
Fore.RED + info['arrTime']+ Fore.RESET]))
# 获取车次站点名称
row_info.append('\n' + '\n'.join([Fore.GREEN + info['deptStationName'] + Fore.RESET,
Fore.RED + info['arrStationName']+ Fore.RESET]))
# 获取车次到达站点所需时间
row_info.append('\n' + info['runTime'])
# 获取票价以及余票信息
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()
# 获取所有站点信息(stations.txt信息通过 函数获取)
# 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)
# 从所有站点信息中获取所要查询站点的代码信息
query_args['to_station'] = stations_info[arguments['']]
query_args['source_station'] = stations_info[arguments['']]
query_args['date'] = arguments['']
# 向12306查询,得到跟车次相关的所有信息
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()
总结
以上所述是小编给大家介绍的使用python脚本查询火车票工具网站的支持!
来源:https://www.cnblogs.com/zpzhue/archive/2018/07/19/9336235.html
标签:python,查询,火车票
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PHP getNamespaces()函数讲解
2023-06-11 07:32:45
![](https://img.aspxhome.com/file/2023/7/55407_0s.png)
制作设置404页面
2009-07-03 13:01:00
![](https://img.aspxhome.com/file/UploadPic/20097/3/200973131448261s.jpg)
Pandas中resample方法详解
2021-10-03 09:57:40
python实现定时压缩指定文件夹发送邮件
2022-06-02 19:32:36
python保留小数位的三种实现方法
2021-09-05 22:36:09
在CentOS 6 中安装WordPress(一) 安装Apache,Mysql, PHP环境
2023-11-06 17:41:24
![](https://img.aspxhome.com/file/2023/2/64962_0s.jpg)
微软工程师讲解SQL server阻塞
2008-01-05 14:02:00
Python如何使用type()函数查看数据的类型
2022-05-30 17:49:31
![](https://img.aspxhome.com/file/2023/1/83861_0s.png)
matplotlib基础绘图命令之imshow的使用
2023-12-27 17:08:58
![](https://img.aspxhome.com/file/2023/4/103604_0s.png)
微信小程序上传图片到php服务器的方法
2023-11-07 11:57:25
python3 使用traceback定位异常实例
2023-05-03 12:42:35
![](https://img.aspxhome.com/file/2023/1/101231_0s.jpg)
黑客谈 MSSQL SA权限入侵的感悟
2008-03-20 10:18:00
python 批量将PPT导出成图片集的案例
2021-09-14 17:52:36
Django实现CAS+OAuth2的方法示例
2023-01-26 17:32:46
oracle数据库导入导出命令使用方法
2023-07-22 19:06:39
Python批量处理图片大小尺寸方法详解
2021-12-11 23:12:07
![](https://img.aspxhome.com/file/2023/3/92533_0s.png)
详解Python进行数据相关性分析的三种方式
2022-05-09 18:50:12
![](https://img.aspxhome.com/file/2023/4/103474_0s.png)
使用python检测主机存活端口及检查存活主机
2021-08-01 05:20:27
![](https://img.aspxhome.com/file/2023/0/80550_0s.jpg)
python使用requests.session模拟登录
2022-09-12 19:17:24
![](https://img.aspxhome.com/file/2023/1/95301_0s.png)
面向站长和网站管理员的Web缓存加速指南[翻译]
2008-04-22 21:04:00
![](https://img.aspxhome.com/file/UploadPic/up/2008042222003437.gif)