python 3.6 tkinter+urllib+json实现火车车次信息查询功能


Posted in Python onDecember 20, 2017

一、概述

妹子工作时需要大量地查询火车车次至南京的信息,包括该车次到达站(南京站or南京南站)、到达时间、出发时间等,然后根据这些信息做下一步工作。

版本结束,趁着间歇期,帮她弄了个简易的批量查询工具,粉色的按钮是给她用的~哈哈哈! (๑*◡*๑)

大概80行代码,主要是:

界面读取待查询车次 - - - - 调用车次信息接口- - - - 解析返回数据 - - - - 组装结果 - - - - 封装到界面(tkinter)

python+tkinter实现界面,详见之前的学习笔记:https://3water.com/article/131059.htm

最终效果图:

python 3.6 tkinter+urllib+json实现火车车次信息查询功能

二、实现

1.界面读取待查询车次

之前总结过使用tkinter实现GUI,详见之前的笔记:https://3water.com/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)

4.组装结果、界面输出      

#组装结果界面输出
  self.result_data_Text.delete(1.0, END)
  head = "车次 南京到达站 到站时间 离站时间  类型"
  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:获取火车至南京信息完成")

总结

以上所述是小编给大家介绍的python 3.6 tkinter+urllib+json实现火车车次信息查询功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
python的id()函数解密过程
Dec 25 Python
Python中使用MELIAE分析程序内存占用实例
Feb 18 Python
python密码错误三次锁定(实例讲解)
Nov 14 Python
Python中将dataframe转换为字典的实例
Apr 13 Python
Python3实现的旋转矩阵图像算法示例
Apr 03 Python
python验证码图片处理(二值化)
Nov 01 Python
Pyspark获取并处理RDD数据代码实例
Mar 27 Python
Python-jenkins 获取job构建信息方式
May 12 Python
解决Python Matplotlib绘图数据点位置错乱问题
May 16 Python
matplotlib之属性组合包(cycler)的使用
Feb 24 Python
让文件路径提取变得更简单的Python Path库
May 27 Python
python多线程方法详解
Jan 18 Python
python实现神经网络感知器算法
Dec 20 #Python
Python代码实现KNN算法
Dec 20 #Python
详解appium+python 启动一个app步骤
Dec 20 #Python
浅谈Django自定义模板标签template_tags的用处
Dec 20 #Python
Python实现感知机(PLA)算法
Dec 20 #Python
详解Python nose单元测试框架的安装与使用
Dec 20 #Python
使用python实现knn算法
Dec 20 #Python
You might like
php下使用SimpleXML 处理XML 文件
2010/02/27 PHP
php获取用户IPv4或IPv6地址的代码
2012/11/15 PHP
使用YUI+Ant 实现JS CSS压缩
2014/09/02 PHP
Yii的Srbac插件用法详解
2016/07/14 PHP
PHP实现网站访问量计数器
2017/10/27 PHP
php redis setnx分布式锁简单原理解析
2020/10/23 PHP
CL vs ForZe BO5 第四场 2.13
2021/03/10 DOTA
Prototype的Class.create函数解析
2011/09/22 Javascript
解析JavaScript中的不可见数据类型
2013/12/02 Javascript
js和jquery中循环的退出和继续学习记录
2014/09/06 Javascript
原生js结合html5制作简易的双色子游戏
2015/03/30 Javascript
jQuery validate+artdialog+jquery form实现弹出表单思路详解
2016/04/18 Javascript
Kindeditor单独调用多图上传实例
2017/07/31 Javascript
详解webpack编译速度提升之DllPlugin
2019/02/05 Javascript
vue中node_modules中第三方模块的修改使用详解
2019/05/31 Javascript
vue 实现动态路由的方法
2020/07/06 Javascript
python编程实现归并排序
2017/04/14 Python
python GUI图形化编程wxpython的使用
2019/07/19 Python
python子线程退出及线程退出控制的代码
2019/10/16 Python
使用matplotlib绘制图例标签中带有公式的图
2019/12/13 Python
pandas使用之宽表变窄表的实现
2020/04/12 Python
关于Keras Dense层整理
2020/05/21 Python
如何解决pycharm调试报错的问题
2020/08/06 Python
详解canvas绘制多张图的排列顺序问题
2019/01/21 HTML / CSS
加拿大著名时装品牌:SOIA & KYO
2016/08/23 全球购物
英国第一的市场和亚马逊替代品:OnBuy
2019/03/16 全球购物
李维斯法国官网:Levi’s法国
2019/07/13 全球购物
大学生求职简历的自我评价范文
2013/10/12 职场文书
促销活动策划方案
2014/01/12 职场文书
电子信息工程专业推荐信
2014/02/14 职场文书
保护环境演讲稿
2014/05/10 职场文书
篮球兴趣小组活动总结
2014/07/07 职场文书
安全保卫工作竞聘材料
2014/08/25 职场文书
初中差生评语
2014/12/29 职场文书
安全生产隐患排查制度
2015/08/05 职场文书
MySQL高级进阶sql语句总结大全
2022/03/16 MySQL