python 3.6 tkinter+urllib+json實(shí)現(xiàn)火車車次信息查詢功能
一、概述
妹子工作時(shí)需要大量地查詢火車車次至南京的信息,包括該車次到達(dá)站(南京站or南京南站)、到達(dá)時(shí)間、出發(fā)時(shí)間等,然后根據(jù)這些信息做下一步工作。
版本結(jié)束,趁著間歇期,幫她弄了個(gè)簡(jiǎn)易的批量查詢工具,粉色的按鈕是給她用的~哈哈哈! (๑*◡*๑)
大概80行代碼,主要是:
界面讀取待查詢車次 - - - - 調(diào)用車次信息接口- - - - 解析返回?cái)?shù)據(jù) - - - - 組裝結(jié)果 - - - - 封裝到界面(tkinter)
python+tkinter實(shí)現(xiàn)界面,詳見之前的學(xué)習(xí)筆記:http://www.dhdzp.com/article/131059.htm
最終效果圖:

二、實(shí)現(xiàn)
1.界面讀取待查詢車次
之前總結(jié)過使用tkinter實(shí)現(xiàn)GUI,詳見之前的筆記:http://www.dhdzp.com/article/131059.htm
2.調(diào)用車次信息接口
題外話,之前是做的從界面讀取待查詢車次信息,然后構(gòu)造成攜程的查詢url,取到數(shù)據(jù)后篩選信息;
但后續(xù)在取到頁面數(shù)據(jù)后,decode時(shí)發(fā)現(xiàn)總拋解碼異常,百度之,原因是頁面源碼中編碼格式有多樣,decode時(shí)需要加個(gè)錯(cuò)誤跳過參數(shù)。。
但車次信息恰巧在跳過之列。。。
但是已經(jīng)跟妹子說很快就能搞好(裝b),于是就直接申請(qǐng)了某第三方平臺(tái)的接口 QAQ
網(wǎng)上查了下,免費(fèi)的接口基本都不提供服務(wù)了。于是用的某第三方平臺(tái)的接口(某速數(shù)據(jù)),注冊(cè)贈(zèng)1000條,續(xù)費(fèi)5元1W條(暫時(shí)沒續(xù)=。=)
#調(diào)用車次信息接口,獲取車次信息
def getTrainScheduleInfo(self,trainSchedule):
trainBaseInfo = ""
#拼接URL
url = "http://api.xxxx.com/train/line?appkey=xxxxxx&trainno=" + trainSchedule
#print(url)
#獲取數(shù)據(jù)
try:
trainBaseInfo = self.send_GET_request(url) #發(fā)送GET請(qǐng)求,python3.X是用urllib.request庫,網(wǎng)上很多
except:
print("ERROR:FUNC getTrainScheduleInfo select_items_from_url failed.url = %s ,flag = %s"%(trainSchedule))
return trainBaseInfo
3.解析返回?cái)?shù)據(jù)
返回?cái)?shù)據(jù)為json類型的字符串,直接json.loads后,解析即可
#獲取所有待查詢車次信息
allTrainResultDic = {} #車次查詢結(jié)果集合
for trainSchedule in trainScheduleList:
trainBaseInfo = self.getTrainScheduleInfo(trainSchedule) #json string
# #----測(cè)試----
# trainBaseInfo = '''{"status":"0","msg":"ok","result":{"trainno":"G8","type":"高鐵","list":[{"sequenceno":"1","station":"上海虹橋","day":"1","arrivaltime":"----","departuretime":"19:00","stoptime":"0","costtime":"0","distance":"0","isend":"0","pricesw":"","pricetd":"","pricegr1":"","pricegr2":"","pricerw1":"0","pricerw2":"0","priceyw1":"0","priceyw2":"0","priceyw3":"0","priceyd":"0.0","priceed":"0.0"},{"sequenceno":"2","station":"南京南","day":"1","arrivaltime":"20:00","departuretime":"20:02","stoptime":"2","costtime":"60","distance":"295","isend":"0","pricesw":"429.5","pricetd":"0","pricegr1":"0","pricegr2":"0","pricerw1":"0","pricerw2":"0","priceyw1":"0","priceyw2":"0","priceyw3":"0","priceyd":"229.5","priceed":"134.5"},{"sequenceno":"3","station":"濟(jì)南西","day":"1","arrivaltime":"21:59","departuretime":"22:01","stoptime":"2","costtime":"179","distance":"0","isend":"0","pricesw":"1263.5","pricetd":"","pricegr1":"","pricegr2":"","pricerw1":"0","pricerw2":"0","priceyw1":"0","priceyw2":"0","priceyw3":"0","priceyd":"673.5","priceed":"398.5"},{"sequenceno":"4","station":"天津南","day":"1","arrivaltime":"22:59","departuretime":"23:01","stoptime":"2","costtime":"239","distance":"0","isend":"0","pricesw":"1603.5","pricetd":"","pricegr1":"","pricegr2":"","pricerw1":"0","pricerw2":"0","priceyw1":"0","priceyw2":"0","priceyw3":"0","priceyd":"853.5","priceed":"508.5"},{"sequenceno":"5","station":"北京南","day":"1","arrivaltime":"23:34","departuretime":"23:34","stoptime":"0","costtime":"274","distance":"0","isend":"1","pricesw":"1748","pricetd":"","pricegr1":"","pricegr2":"","pricerw1":"0","pricerw2":"0","priceyw1":"0","priceyw2":"0","priceyw3":"0","priceyd":"933.0","priceed":"553.0","costtimetxt":"4時(shí)34分"}]}}'''
# #----測(cè)試----
print("trainBaseInfo =",trainBaseInfo)
#解析
if trainBaseInfo:
try:
trainBaseInfo_loads = json.loads(trainBaseInfo)
if trainBaseInfo_loads["status"] == "0":
resultNodeValue = trainBaseInfo_loads["result"]
trainnoNodeValue = resultNodeValue["trainno"] #查詢車次代碼
typeNodeValue = resultNodeValue["type"] #車次類型
listNodeValue = resultNodeValue["list"] #途徑站點(diǎn)信息集合 list
#篩選出途經(jīng)南京、南京南
for trainInfo in listNodeValue:
if (cityName1 in trainInfo.values()) or (cityName2 in trainInfo.values()):
#解析數(shù)據(jù)
arrivedStation = trainInfo["station"] #到達(dá)站
arrivedTime = trainInfo["arrivaltime"] #到站時(shí)間
leaveTime = trainInfo["departuretime"] #離站時(shí)間
if arrivedStation == "南京":
arrivedStation = "南京站"
# 存儲(chǔ)該車次查詢結(jié)果
trainResult = []
trainResult.append(arrivedStation)
trainResult.append(arrivedTime)
trainResult.append(leaveTime)
trainResult.append(typeNodeValue)
allTrainResultDic[trainSchedule] = trainResult
else:
#self.write_log_to_Text("ERROR:車次: %s 無途徑南京站信息,跳過" % trainSchedule)
continue
else:
self.write_log_to_Text("ERROR:車次: %s 檢查返回?cái)?shù)據(jù)狀態(tài)碼不為0,跳過" % trainSchedule)
continue
except:
self.write_log_to_Text("ERROR:車次:%s 返回的json串失敗 "% trainSchedule)
else:
self.write_log_to_Text("ERROR:車次: %s 查詢接口返回信息為空,已跳過"%trainSchedule)
continue
print(allTrainResultDic)
4.組裝結(jié)果、界面輸出
#組裝結(jié)果界面輸出
self.result_data_Text.delete(1.0, END)
head = "車次 南京到達(dá)站 到站時(shí)間 離站時(shí)間 類型"
self.result_data_Text.insert(1.0, head)
for train in allTrainResultDic.keys():
outMsg = "\n" + "-" * 52 + "\n" + "%4s"%train + "%9s"%allTrainResultDic[train][0] + "%13s"%allTrainResultDic[train][1] + "%12s"%allTrainResultDic[train][2] + "%8s"%allTrainResultDic[train][3]
self.result_data_Text.insert(END,outMsg)
self.write_log_to_Text("INFO:獲取火車至南京信息完成")
總結(jié)
以上所述是小編給大家介紹的python 3.6 tkinter+urllib+json實(shí)現(xiàn)火車車次信息查詢功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- python?接口返回的json字符串實(shí)例
- 使用Django和Python創(chuàng)建Json response的方法
- Python實(shí)現(xiàn)將json文件中向量寫入Excel的方法
- python 對(duì)象和json互相轉(zhuǎn)換方法
- 利用python將json數(shù)據(jù)轉(zhuǎn)換為csv格式的方法
- python如何讀寫json數(shù)據(jù)
- python2.7 json 轉(zhuǎn)換日期的處理的示例
- python3.x上post發(fā)送json數(shù)據(jù)
- python使用json序列化datetime類型實(shí)例解析
- Python Json序列化與反序列化的示例
- Python實(shí)現(xiàn)JSON反序列化類對(duì)象的示例
- Python簡(jiǎn)單讀取json文件功能示例
- 深入分析python數(shù)據(jù)挖掘 Json結(jié)構(gòu)分析
相關(guān)文章
解決pip安裝報(bào)錯(cuò)required?to?install?pyproject.toml-based?projec
這篇文章主要介紹了解決pip安裝報(bào)錯(cuò)required?to?install?pyproject.toml-based?projects問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Python開發(fā)畢設(shè)案例之桌面學(xué)生信息管理程序
畢業(yè)設(shè)計(jì)必備案例:Python開發(fā)桌面程序2021-11-11
Python Opencv中用compareHist函數(shù)進(jìn)行直方圖比較對(duì)比圖片
這篇文章主要介紹了Python Opencv中用compareHist函數(shù)進(jìn)行直方圖比較進(jìn)行對(duì)比圖片,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Python+樹莓派+YOLO打造一款人工智能照相機(jī)
今天,我們將自己動(dòng)手打造出一款基于深度學(xué)習(xí)的照相機(jī),當(dāng)小鳥出現(xiàn)在攝像頭畫面中時(shí),它將能檢測(cè)到小鳥并自動(dòng)進(jìn)行拍照2018-01-01
Python標(biāo)準(zhǔn)庫之Math,Random模塊使用詳解
math數(shù)學(xué)模塊和random隨機(jī)模塊是Python常用的標(biāo)準(zhǔn)庫之一。本文將詳細(xì)為大家介紹一下這兩個(gè)模塊的使用方法,需要的小伙伴可以參考一下2022-05-05
《Python之禪》中對(duì)于Python編程過程中的一些建議
這篇文章主要介紹了《Python之禪》中對(duì)于Python編程過程中的一些建議,需要的朋友可以參考下2015-04-04
使用Python實(shí)現(xiàn)遺傳算法的完整代碼
這篇文章主要介紹了使用Python實(shí)現(xiàn)遺傳算法,其本質(zhì)是一種高效、并行、全局搜索的方法,自適應(yīng)的控制搜索過程以求得最優(yōu)解,需要的朋友可以參考下2023-03-03

