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 getopt模块处理命令行选项实例
May 13 Python
Python标准库之随机数 (math包、random包)介绍
Nov 25 Python
python的类方法和静态方法
Dec 13 Python
在Python的web框架中编写创建日志的程序的教程
Apr 30 Python
pycharm中连接mysql数据库的步骤详解
May 02 Python
代码分析Python地图坐标转换
Feb 08 Python
python 获取当天每个准点时间戳的实例
May 22 Python
python绘制直线的方法
Jun 30 Python
对tensorflow 的模型保存和调用实例讲解
Jul 28 Python
使用python实现抓取腾讯视频所有电影的爬虫
Apr 15 Python
Python3.6实现根据电影名称(支持电视剧名称),获取下载链接的方法
Aug 26 Python
解决pytorch多GPU训练保存的模型,在单GPU环境下加载出错问题
Jun 23 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 和 MySQL 基础教程(四)
2006/10/09 PHP
PHP模板引擎smarty详细介绍
2015/05/26 PHP
Ajax PHP JavaScript MySQL实现简易无刷新在线聊天室
2016/08/17 PHP
详解php语言最牛掰的Laravel框架
2017/11/20 PHP
PHP PDOStatement::debugDumpParams讲解
2019/01/30 PHP
jquery 插件开发备注
2010/08/27 Javascript
jQuery 源码分析笔记(4) Ready函数
2011/06/02 Javascript
Css3制作变形与动画效果
2015/07/24 Javascript
js弹出对话框方式小结
2015/11/17 Javascript
IE和Firefox之间在JavaScript语法上的差异
2016/04/22 Javascript
JavaScript操作文件_动力节点Java学院整理
2017/06/30 Javascript
基于Vue过渡状态实例讲解
2017/09/14 Javascript
Vue 去除路径中的#号
2018/04/19 Javascript
elementUI 动态生成几行几列的方法示例
2019/07/11 Javascript
VScode格式化ESlint方法(最全最好用方法)
2019/09/10 Javascript
Vue组件跨层级获取组件操作
2020/07/27 Javascript
分享8个JavaScript库可更好地处理本地存储
2020/10/12 Javascript
vue 图片裁剪上传组件的实现
2020/11/12 Javascript
[15:09]DOTA2国际邀请赛采访专栏:Loda
2013/08/06 DOTA
Python脚本实现下载合并SAE日志
2015/02/10 Python
简单介绍Python2.x版本中的cmp()方法的使用
2015/05/20 Python
浅谈python中requests模块导入的问题
2018/05/18 Python
Flask模拟实现CSRF攻击的方法
2018/07/24 Python
在django模板中实现超链接配置
2019/08/21 Python
基于Tensorflow一维卷积用法详解
2020/05/22 Python
Python编写memcached启动脚本代码实例
2020/08/14 Python
IE10 Error.stack 让脚本调试更加方便快捷
2013/04/22 HTML / CSS
Tiqets荷兰:出售欧洲最美丽的景点和博物馆门票
2018/01/09 全球购物
企业诚信承诺书
2014/05/23 职场文书
党员干部形式主义个人整改措施
2014/09/17 职场文书
高一地理教学工作总结
2015/08/12 职场文书
实验心得体会范文
2016/01/25 职场文书
MySQL 角色(role)功能介绍
2021/04/24 MySQL
关于mysql中时间日期类型和字符串类型的选择
2021/11/27 MySQL
JavaScript选择器函数querySelector和querySelectorAll
2021/11/27 Javascript
详解Golang如何优雅的终止一个服务
2022/03/21 Golang