使用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制作查询火车票工具的更多相关文章

  1. 使用python制作时间戳转换工具

    使用python制作时间戳转换工具 python 时间戳转日期 日期转时间戳 前言:作为一个程序员一般情况下,json和时间戳是常用的两个工具,我咨询过很多个朋友,他们一般都是通过在线工具对json进 ...

  2. 《零基础学习Python制作ArcGIS自定义工具》课程简介

    Python for ArcGIS Python for ArcGIS是借助Python语言实现ArcGIS自动化行为的综合,它不止是如课程标题所述的“制作ArcGIS自定义工具”,还包括使用Pyth ...

  3. python制作命令行工具——fire

    python制作命令行工具--fire 前言 本篇教程的目的是希望大家可以通读完此篇之后,可以使用python制作一款符合自己需求的linux工具. 本教程使用的是google开源的python第三方 ...

  4. 用python制作文件搜索工具,深挖电脑里的【学习大全】

    咳咳~懂得都懂啊 点击此处找管理员小姐姐领取正经资料~ 开发环境 解释器: Python 3.8.8 | Anaconda, Inc. 编辑器: pycharm 专业版 先演示效果 开始代码,先导入模 ...

  5. 使用python制作ArcGIS插件(1)工具介绍

    使用python制作ArcGIS插件(1)工具介绍 by 李远祥 ArcGIS从10.0开始支持addin(ArcGIS软件中又叫作加载项)的方式进行插件制作.相对于以往9.x系列,addin的无论是 ...

  6. 小工具:天气查询 Vs自定义设置 DevGridControl中GridView排序问题 小工具:火车票查询 小工具:邮件发送 小工具:截图&简单图像处理

    小工具:天气查询   开发一个天气查询的工具主要由两步构成,一是数据的获取,二是数据的展示.  一.数据获取 数据获取又可以分为使用其它公司提供的API和手动抓取其它网站数据. 1. 某公司提供的AP ...

  7. Python的包管理工具

    Python的包管理工具 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.为什么使用包管理 Python的模块或者源文件直接可以复制到目标项目目录中,就可以导入使用了. 但是为了 ...

  8. 使用python制作ArcGIS插件(5)其他技巧

    使用python制作ArcGIS插件(5)其他技巧 by 李远祥 使用python做插件开发,除了了解ArcToolBox工具之外,还需要在了解ArcPy的相关函数和接口.只有掌握了这些,才可以顺利的 ...

  9. 使用python制作ArcGIS插件(4)界面交互

    使用python制作ArcGIS插件(4)界面交互 by 李远祥 插件界面部分,除了一开始在设计器中设计的这些界面元素之外,还可以与操作系统进行一些输入输出的交互,这部分的实现全部在pythonadd ...

随机推荐

  1. TopJUI通过简单的代码实现复杂的批量提交功能

    业务系统的批量提交是常用的操作功能,使用传统的EasyUI开发时需要写不少代码才能实现,该功能在TopJUI中是如何实现的呢?本篇我们将通过简单的代码,把批量操作的具体实现分享给大家参考. <a ...

  2. python如何用pip安装模块

    pip去python官网下载 我想写的是安装后怎么做,假设我们要安装pymysql模块 在python交互式模式中运行pip install pymysql 会抛出 语法错误,不知为何. 此时应该找到 ...

  3. jsp 预编译

    jspc-maven-plugin <?xml version="1.0" encoding="UTF-8"?> <project xmlns ...

  4. 测试 | 单元测试工具 | JUnit | 参数化

    被测试类: package project; public class MyCalendar2 { public int getNumberOfDaysInMonth(int year, int mo ...

  5. ShareSDK集成遇到问题

    解决方案

  6. ASP.NET经典的、封装好的ADO.NET类包

    using System; using System.Collections; using System.Collections.Specialized; using System.Runtime.R ...

  7. python对文件的压缩解压

    python自带的zipfile的模块支持对文件的压缩和解压操作 zipfilp.ZipFile 表示创建一个zip对象 zipfile.ZipFile(file[, mode[, compressi ...

  8. if __FILE__ == $0 end

    if __FILE__ == $0 end __FILE__是一个“具有魔力”的变量,它代表了当前文件名.$0是用于启动程序的文件名.那么代码“if __FILE__ == $0”便意味着检查此文件是 ...

  9. JS添加事件和解绑事件:addEventListener()与removeEventListener()

    作用: addEventListener()与removeEventListener()用于处理指定和删除事件处理程序操作. 它们都接受3个参数:事件名.事件处理的函数和布尔值. 布尔值参数是true ...

  10. leetcode128 Longest Consecutive Sequence

    思路: 维护一个unordered_map,key是每个连续区间的端点,value是该区间的长度. 实现: class Solution { public: int longestConsecutiv ...