ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

使用python制作查询火车票工具

2021-09-06 15:04:15  阅读:233  来源: 互联网

标签:info python ret 查询 Fore query seat 火车票 row


使用python脚本实现查询火车票信息的效果图如下:

使用python制作查询火车票工具

实现的代码:

1 # coding: utf-8

2

3 “”"命令行火车票查看器

4

5 Usage:

6 tickets [-gdtkz]

7

8 Options:

9 -h,–help 显示帮助菜单

10 -g 高铁

11 -d 动车

12 -t 特快

13 -k 快速

14 -z 直达

15

16 Example:

17 tickets 北京 上海 2016-10-10

18 tickets -dg 成都 南京 2016-10-10

19 “”"

20

21 import json

22 import requests

23 import prettytable

24 from docopt import docopt

25 from colorama import init, Fore

26

27

28 class CollectInfo:

29 def init(self):

30 self.qurey_ret = []

31 self.header = [‘车次信息’, ‘发/到时间’, ‘发/到站’, ‘历时’, ‘票价’, ‘余票’]

32

33 # 获取车次相关的所有信息

34 def query_html_ret(self, query_args):

35 url = 'http://api.12306.com/v1/train/trainInfos?arrStationCode={to_station}&deptDate={date}\

36 &deptStationCode={source_station}&findGD=false’.format(to_station=query_args[‘to_station’],

37 source_station=query_args[‘source_station’],

38 date=query_args[‘date’])

39 row_ret = requests.get(url)

40 return row_ret.json()

41

42 # 解析获取到的结果

43 def paser_ret(self, row_ret):

44 trains_info = row_ret[‘data’][‘trainInfos’]

45 for info in trains_info:

46 row_info = []

47 # 获取车次信息

48 row_info.append(’\n’ + info[‘trainCode’])

49

50 # 获取车次到站时间信息

51 row_info.append(’\n’ + ‘\n’.join([Fore.GREEN + info[‘deptTime’]+ Fore.RESET,

52 Fore.RED + info[‘arrTime’]+ Fore.RESET]))

53

54 # 获取车次站点名称

55 row_info.append(’\n’ + ‘\n’.join([Fore.GREEN + info[‘deptStationName’] + Fore.RESET,

56 Fore.RED + info[‘arrStationName’]+ Fore.RESET]))

57

58 # 获取车次到达站点所需时间

59 row_info.append(’\n’ + info[‘runTime’])

60

61 # 获取票价以及余票信息

62 seat_price = []

63 seat_num = []

64 for seat in info[‘seatList’]:

65 seat_price.append(seat[‘seatName’] + ‘:’ + seat[‘seatPrice’])

66 if int(seat[‘seatNum’]) > 10:

67 ticknum = Fore.GREEN + seat[‘seatNum’] + Fore.RESET

68 else:

69 ticknum = seat[‘seatNum’]

70 seat_num.append(ticknum)

71 row_info.append(’\n’.join(seat_price))

72 row_info.append(’\n’.join(seat_num))

73 self.qurey_ret.append(row_info)

74 self.qurey_ret.append([’ ', ’ ', ’ ', ’ ', ’ ', ’ '])

75

76 return self.qurey_ret

77

78 def show_with_table(self):

79 ticket_table = prettytable.PrettyTable()

80

81 ticket_table.field_names = self.header

82

83 for row in self.qurey_ret:

84 if len(row) == 0:

85 continue

86 ticket_table.add_row(row)

87 return ticket_table

88

89

90 def main():

91 arguments = docopt(doc)

92 query_args = {}

93 init()

94

95 # 获取所有站点信息(stations.txt信息通过 函数获取)

96 # https: // kyfw.12306.cn / otn / resources / js / framework / station_name.js?station_version = 1.8971

97 f = open(‘stations.txt’, ‘r’)

98 info = f.read()

99 stations_info = json.loads(info)

100

101 # 从所有站点信息中获取所要查询站点的代码信息

102 query_args[‘to_station’] = stations_info[arguments[’’]]

103 query_args[‘source_station’] = stations_info[arguments[’’]]

104 query_args[‘date’] = arguments[’’]

105

106 # 向12306查询,得到跟车次相关的所有信息

107 collect_train = CollectInfo()

108 row_ret = collect_train.query_html_ret(query_args)

109

110 collect_train.paser_ret(row_ret)

111 table = collect_train.show_with_table()

112 print(table)

113

114

115 if name == ‘main’:

116 main()

使用python制作查询火车票工具

标签:info,python,ret,查询,Fore,query,seat,火车票,row
来源: https://blog.csdn.net/kj7762/article/details/120134939

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有