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中给List添加元素的4种方法分享
Nov 28 Python
Python yield 使用方法浅析
May 20 Python
Pycharm 操作Django Model的简单运用方法
May 23 Python
用Python画一个LinkinPark的logo代码实例
Sep 10 Python
Python-numpy实现灰度图像的分块和合并方式
Jan 09 Python
TensorBoard 计算图的查看方式
Feb 15 Python
浅谈图像处理中掩膜(mask)的意义
Feb 19 Python
Pyecharts 动态地图 geo()和map()的安装与用法详解
Mar 25 Python
OpenCV 表盘指针自动读数的示例代码
Apr 10 Python
django创建css文件夹的具体方法
Jul 31 Python
python用tkinter实现一个简易能进行随机点名的界面
Sep 27 Python
numpy数据类型dtype转换实现
Apr 24 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中关于codeigniter的xmlrpc的类在进行数据交换时的类型问题
2011/07/03 PHP
PHP简单选择排序算法实例
2015/01/26 PHP
ThinkPHP实现分页功能
2017/04/28 PHP
PHP获取数组中指定的一列实例
2017/12/27 PHP
基于Web标准的UI组件 — 树状菜单(2)
2006/09/18 Javascript
关于使用runtimeStyle属性问题讨论文章
2007/03/08 Javascript
Firefox window.close()的使用注意事项
2009/04/11 Javascript
Javascript的匿名函数小结
2009/12/31 Javascript
TinyMCE 新增本地图片上传功能
2010/11/05 Javascript
jQuery EasyUI API 中文文档 - Draggable 可拖拽
2011/09/29 Javascript
javascript中解析四则运算表达式的算法和示例
2014/08/11 Javascript
AngularJS 日期格式化详解
2015/12/23 Javascript
JavaScript禁止用户多次提交的两种方法
2016/07/24 Javascript
js插件dropload上拉下滑加载数据实例解析
2016/07/27 Javascript
Angularjs 自定义服务的三种方式(推荐)
2016/08/02 Javascript
浅析script标签中的defer与async属性
2016/11/30 Javascript
JS正则表达式验证账号、手机号、电话和邮箱是否合法
2017/03/08 Javascript
快速搭建vue2.0+boostrap项目的方法
2018/04/09 Javascript
Python的collections模块中namedtuple结构使用示例
2016/07/07 Python
Python简单实现安全开关文件的两种方式
2016/09/19 Python
Python使用matplotlib的pie函数绘制饼状图功能示例
2018/01/08 Python
解决python删除文件的权限错误问题
2018/04/24 Python
解决tensorflow测试模型时NotFoundError错误的问题
2018/07/26 Python
python 异或加密字符串的实例
2018/10/14 Python
python实现的接收邮件功能示例【基于网易POP3服务器】
2019/09/11 Python
python中实现词云图的示例
2020/12/19 Python
ProBikeKit新西兰:自行车套件,跑步和铁人三项装备
2017/04/05 全球购物
美国价格实惠的在线眼镜网站:Zeelool
2020/12/25 全球购物
文体活动总结范文
2014/05/05 职场文书
三年级学生期末评语
2014/12/26 职场文书
租车协议书
2015/01/27 职场文书
2015年税务稽查工作总结
2015/05/26 职场文书
2016大学生形势与政策心得体会
2016/01/12 职场文书
《酸的和甜的》教学反思
2016/02/18 职场文书
mysql5.7使用binlog 恢复数据的方法
2021/06/03 MySQL
Python selenium绕过webdriver监测执行javascript
2022/04/12 Python