python 3 12306餘票查詢指令碼
阿新 • • 發佈:2019-01-11
原文地址 https://blog.csdn.net/memory_qianxiao/article/details/81944732 直接帖程式碼,不懂的看原文,只是注意下url地址更新了,原來那個地址好像訪問不了,然後加上了時間輸入 #-*- coding:utf-8 -*- import requests,re,datetime,time,json,datetime from tkinter import* from tkinter import messagebox from prettytable import PrettyTable #這個庫是自己寫的,把所有車站提取出來的.py檔案,我也會放在下面。 from stationsInfo import station_codes,station_names from colorama import init,Fore,Back,Style def getHtmltext(url,headers): r=requests.get(url,headers=headers) r.raise_for_status() r.encoding=r.apparent_encoding return r.text init(autoreset=False) class Colored(object): # 前景色:紅色 背景色:預設 def red(self, s): return Fore.LIGHTRED_EX + s + Fore.RESET # 前景色:綠色 背景色:預設 def green(self, s): return Fore.LIGHTGREEN_EX + s + Fore.RESET def yellow(self, s): return Fore.LIGHTYELLOW_EX + s + Fore.RESET def white(self,s): return Fore.LIGHTWHITE_EX + s + Fore.RESET def blue(self,s): return Fore.LIGHTBLUE_EX + s + Fore.RESET def showTicket(html): html=json.loads(html) table=PrettyTable(["車次","出發/到達車站","出發/到達時間","歷時","商務座","一等座","二等座","高階軟臥","軟臥","動臥","硬臥","軟座","硬座","無座","其他","備註"]) for i in html['data']['result']: name = [ "station_train_code", "from_station_name", 'start_time', "lishi", "swz_num", "zy_num", "ze_num", "gr_num", "rw_num", "dw_num", "yw_num", "rz_num", "yz_num", "wz_num", "qt_num", "note_num" ] data={ "station_train_code": '', "from_station_name": '', "to_station_name": '', 'start_time': '', 'end': '', "lishi": '', "swz_num": '', "zy_num": '', "ze_num": '', "dw_num": '', "gr_num": '', "rw_num": '', "yw_num": '', "rz_num": '', "yz_num": '', "wz_num": '', "qt_num": '', "note_num": '' } item = i.split('|')#用"|"進行分割 data['station_train_code'] = item[3]#車次在3號位置 data['from_station_name'] = item[6]#始發站資訊在6號位置 data['to_station_name'] = item[7]#終點站資訊在7號位置 data['start_time'] = item[8]#出發時間資訊在8號位置 data['arrive_time'] = item[9]#抵達時間在9號位置 data['lishi'] = item[10]#經歷時間在10號位置 data['swz_num'] = item[32] or item[25]# 特別注意:商務座在32或25位置 data['zy_num'] = item[31]#一等座資訊在31號位置 data['ze_num'] = item[30]#二等座資訊在30號位置 data['gr_num'] = item[21]#高階軟臥資訊在31號位置 data['rw_num'] = item[23]#軟臥資訊在23號位置 data['dw_num'] = item[27]#動臥資訊在27號位置 data['yw_num'] = item[28]#硬臥資訊在28號位置 data['rz_num'] = item[24]#軟座資訊在24號位置 data['yz_num'] = item[29]#硬座資訊在29號位置 data['wz_num'] = item[26]#無座資訊在26號位置 data['qt_num'] = item[22]#其他資訊在22號位置 data['note_num'] = item[1]#備註在1號位置 color = Colored()#建立Colored物件 data["note_num"]=color.blue(item[1]) #如果沒有資訊用'-'代替 for pos in name: if data[pos]=='': data[pos]='-' tickets=[] cont=[] cont.append(data) for x in cont: tmp = [] for y in name: if y == "from_station_name": s = color.green(station_names[data['from_station_name']])+ '\n' +color.red(station_names[data["to_station_name"]]) tmp.append(s) elif y == "start_time": s = color.green(data['start_time']) + '\n' + color.red(data["arrive_time"]) tmp.append(s) elif y == "station_train_code": s = color.yellow(data['station_train_code']) tmp.append(s) else: tmp.append(data[y]) tickets.append(tmp) for ticket in tickets: table.add_row(ticket) print(table) def main(): from_station=station_codes[input("請輸入起始站:\n")] to_station=station_codes[input("請輸入目地站:\n")] dates = input("請輸入日期:\n") timeArray = time.strptime(dates,"%Y-%m-%d") dta = time.strftime("%Y-%m-%d", timeArray) url="https://kyfw.12306.cn/otn/leftTicket/queryZ?" headers={ "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5702.400 QQBrowser/10.2.1893.400" } #time=entry_time.get() #start=entry_start.get() #end=entry_end.get()sh url=url+'leftTicketDTO.train_date='+dta+'&leftTicketDTO.from_station='+from_station+'&leftTicketDTO.to_station='+to_station+'&purpose_codes=ADULT' print(url) html=getHtmltext(url,headers) showTicket(html) main()
stationsInfo 程式碼 #-*- coding:utf-8 -*- import re, requests url = 'https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.8971' response = requests.get(url, verify=False) stations = re.findall(r'([\u4e00-\u9fa5]+)\|([A-Z]+)', response.text) station_codes = dict(stations) station_names = dict(zip(station_codes.values(), station_codes.keys()))