国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python 3.6 tkinter+urllib+json實現火車車次信息查詢功能

2020-02-16 11:12:56
字體:
來源:轉載
供稿:網友

一、概述

妹子工作時需要大量地查詢火車車次至南京的信息,包括該車次到達站(南京站or南京南站)、到達時間、出發時間等,然后根據這些信息做下一步工作。

版本結束,趁著間歇期,幫她弄了個簡易的批量查詢工具,粉色的按鈕是給她用的~哈哈哈! (๑*◡*๑)

大概80行代碼,主要是:

界面讀取待查詢車次 - - - - 調用車次信息接口- - - - 解析返回數據 - - - - 組裝結果 - - - - 封裝到界面(tkinter)

python+tkinter實現界面,詳見之前的學習筆記://www.jb51.net/article/131059.htm

最終效果圖:

二、實現

1.界面讀取待查詢車次

之前總結過使用tkinter實現GUI,詳見之前的筆記://www.jb51.net/article/131059.htm

2.調用車次信息接口

題外話,之前是做的從界面讀取待查詢車次信息,然后構造成攜程的查詢url,取到數據后篩選信息;

但后續在取到頁面數據后,decode時發現總拋解碼異常,百度之,原因是頁面源碼中編碼格式有多樣,decode時需要加個錯誤跳過參數。。

但車次信息恰巧在跳過之列。。。

但是已經跟妹子說很快就能搞好(裝b),于是就直接申請了某第三方平臺的接口 QAQ

網上查了下,免費的接口基本都不提供服務了。于是用的某第三方平臺的接口(某速數據),注冊贈1000條,續費5元1W條(暫時沒續=。=)

 #調用車次信息接口,獲取車次信息 def getTrainScheduleInfo(self,trainSchedule):  trainBaseInfo = ""  #拼接URL  url = "http://api.xxxx.com/train/line?appkey=xxxxxx&trainno=" + trainSchedule  #print(url)  #獲取數據  try:   trainBaseInfo = self.send_GET_request(url)  #發送GET請求,python3.X是用urllib.request庫,網上很多  except:   print("ERROR:FUNC getTrainScheduleInfo select_items_from_url failed.url = %s ,flag = %s"%(trainSchedule))  return trainBaseInfo

3.解析返回數據

返回數據為json類型的字符串,直接json.loads后,解析即可

#獲取所有待查詢車次信息  allTrainResultDic = {}  #車次查詢結果集合  for trainSchedule in trainScheduleList:   trainBaseInfo = self.getTrainScheduleInfo(trainSchedule)  #json string   # #----測試----   # 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":"濟南西","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時34分"}]}}'''   # #----測試----   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"]   #途徑站點信息集合 list      #篩選出途經南京、南京南      for trainInfo in listNodeValue:       if (cityName1 in trainInfo.values()) or (cityName2 in trainInfo.values()):        #解析數據        arrivedStation = trainInfo["station"]   #到達站        arrivedTime = trainInfo["arrivaltime"]  #到站時間        leaveTime = trainInfo["departuretime"]  #離站時間        if arrivedStation == "南京":         arrivedStation = "南京站"          # 存儲該車次查詢結果        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 檢查返回數據狀態碼不為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)            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 敦煌市| 郸城县| 海门市| 曲麻莱县| 波密县| 孟连| 建德市| 长寿区| 景德镇市| 廊坊市| 来宾市| 甘洛县| 那曲县| 海门市| 嘉峪关市| 景泰县| 乐都县| 介休市| 五华县| 宜良县| 措美县| 宿松县| 施秉县| 兴宁市| 南城县| 林西县| 莲花县| 濮阳县| 蒙城县| 肇州县| 墨玉县| 扎兰屯市| 修武县| 定南县| 手游| 大新县| 喀喇| 吴堡县| 福海县| 朝阳市| 宽城|