Python requests +PrettyTable 查询高铁或者动车票
1、为什么只查询高铁或者动车,因为我现在基本上不会选择坐K字头的火车了
2、12306网站截取一条查询信息
3、response数据里的result,每一条都对应一次列车信息,返回的数据以“”|“”分割,37条数据
4、随便拿一条解析下,有些对比网站看出来了,有些不知道,但是不影响我们这次的目的
5、第 5 6 7 8 条数据代表的是车站的代码,不知道怎么查,但是网上有对应的 txt文档,拿过来直接读
6、使用prettytable 把数据打印出来,很工整,可以应用在UI测试,表格数据的获取,打印到日志中
7、上代码
#encoding:gbk import requests from prettytable import PrettyTable import time import sys class checkTickets : def __init__(self): print("Welcome lilin Zone") self.train_date = "" self.from_station = "" self.to_station = "" self.url = "" #加载车站信息 self.stations,self.code_name = self.get_stations_code() def set_train_date(self): curdate = self.get_currtime(2) train_date = input("请输入时间 : ") if train_date == "": print("默认设置查询日期为今日:" + curdate) self.train_date = curdate elif not self.is_valid_date(train_date): print("日期格式输入错误,请按 2018-05-13 格式输入") self.set_train_date() elif train_date < curdate: print("查询时间小于今天,请检查后再输入") self.set_train_date() else: self.train_date = train_date def set_from_station(self): from_name = input("请输入始发站 : ") try: code = self.stations[from_name] return code except: print("查无此站,请检查后重新输入") self.set_from_station() def set_to_station(self): to_name = input("请输入终点站 : ") try: code = self.stations[to_name] return code except: print("查无此站,请检查后重新输入") self.set_to_station() def get_stations_code(self): f = open("d://stationcode.txt") name_code = dict({}) code_name = dict({}) lines = f.readlines() for line in lines: station = line.split(":") name_code[station[0]] = station[1].replace("\n", "") code_name[station[1].replace("\n", "")] = station[0] f.close() return name_code,code_name def get_currtime(self,type = 1): if type == 1: return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) elif type == 2: return time.strftime("%Y-%m-%d", time.localtime()) def is_valid_date(self,strdate): '''''判断是否是一个有效的日期字符串''' try: if ":" in strdate: time.strptime(strdate, "%Y-%m-%d %H:%M:%S") else: time.strptime(strdate, "%Y-%m-%d") return True except: return False def set_urlInfo(self): self.set_train_date() from_station_code = self.set_from_station() to_station_code = self.set_to_station() self.url = "https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date="+self.train_date+"&leftTicketDTO.from_station="+from_station_code+"&leftTicketDTO.to_station="+to_station_code+"&purpose_codes=ADULT" def show_ticketInfo(self): r = requests.get(self.url) #返回的数据 try: response = r.json() except: print("查询出现异常,再次查询") self.show_ticketInfo() #列车信息表 a = response["data"]["result"] if len(a) == 0 and r.status_code == 200: print("已经查不到今天高铁动车班次。本次查询结束,谢谢使用") sys.exit() table = PrettyTable() # ["车次","始发站","终点站","出发时间","到达时间","历时","商务座","一等座","二等座","无座"] table.field_names = ["Train Number","From Station","To Station","Starting Time","Ending Time","Duration Time","Business Class","First Class","Second Class","No Seat"] table.align = 'l' for t_info in a: info = t_info.split("|") t_name = info[3] t_type = t_name[0:1] if t_type == "G" or t_type == "D": table.add_row([info[3],self.code_name[info[6]],self.code_name[info[7]],info[8],info[9],info[10],info[32],info[31],info[30],info[26]]) print(table) if __name__ == "__main__": c = checkTickets() c.set_urlInfo() c.show_ticketInfo()