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中精确输出JSON浮点数的方法
Apr 18 Python
Python实现的生成自我描述脚本分享(很有意思的程序)
Jul 18 Python
零基础写python爬虫之urllib2使用指南
Nov 05 Python
python检测远程服务器tcp端口的方法
Mar 14 Python
Python学习笔记整理3之输入输出、python eval函数
Dec 14 Python
用python写的一个wordpress的采集程序
Feb 27 Python
利用Python画ROC曲线和AUC值计算
Sep 19 Python
python pandas修改列属性的方法详解
Jun 09 Python
Numpy对数组的操作:创建、变形(升降维等)、计算、取值、复制、分割、合并
Aug 28 Python
python 3.74 运行import numpy as np 报错lib\site-packages\numpy\__init__.py
Oct 06 Python
Python Tensor FLow简单使用方法实例详解
Jan 14 Python
pycharm 复制代码出现空格的解决方式
Jan 15 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获取短链接跳转后的真实地址和响应头信息的方法
2014/07/25 PHP
Thinkphp实现自动验证和自动完成
2015/12/19 PHP
WordPress中调试缩略图的相关PHP函数使用解析
2016/01/07 PHP
PHP从尾到头打印链表实例讲解
2018/09/27 PHP
对YUI扩展的Gird组件 Part-2
2007/03/10 Javascript
javascript Array.remove() 数组删除
2009/08/06 Javascript
jquery ajax 局部无刷新更新数据的实现案例
2014/02/08 Javascript
jQuery文件上传插件Uploadify使用指南
2014/06/05 Javascript
AngularJS初始化过程分析(引导程序)
2014/12/06 Javascript
js面向对象之静态方法和静态属性实例分析
2015/01/10 Javascript
jQuery基于扩展实现的倒计时效果
2016/05/14 Javascript
14 个折磨人的 JavaScript 面试题
2016/08/08 Javascript
javascript中异常处理案例(推荐)
2016/10/03 Javascript
BootStrap轮播HTML代码(推荐)
2016/12/10 Javascript
highcharts 在angular中的使用示例代码
2017/09/20 Javascript
JS使用正则表达式获取小括号、中括号及花括号内容的方法示例
2018/06/01 Javascript
Vue中保存数据到磁盘文件的方法
2018/09/06 Javascript
vue插槽slot的理解和使用方法
2019/04/03 Javascript
js根据后缀判断文件文件类型的代码
2020/05/09 Javascript
python网络编程学习笔记(六):Web客户端访问
2014/06/09 Python
wxPython学习之主框架实例
2014/09/28 Python
Django日志模块logging的配置详解
2017/02/14 Python
离线安装Pyecharts的步骤以及依赖包流程
2020/04/23 Python
Python3实现抓取javascript动态生成的html网页功能示例
2017/08/22 Python
浅析Python3爬虫登录模拟
2018/02/07 Python
Python实现微信自动好友验证,自动回复,发送群聊链接方法
2019/02/21 Python
Pytorch模型转onnx模型实例
2020/01/15 Python
解决pytorch 的state_dict()拷贝问题
2021/03/03 Python
世界领先的26岁以下学生和青少年旅行预订网站:StudentUniverse
2018/07/01 全球购物
Bally澳大利亚官网:瑞士奢侈品牌
2018/11/01 全球购物
创造美妙香氛体验:Aera扩散器和香水
2018/11/25 全球购物
某IT外企面试题-二分法求方程!看看大家的C++功底
2015/07/04 面试题
2015年三年级班主任工作总结
2015/05/21 职场文书
茶花女读书笔记
2015/06/29 职场文书
母婴行业实体、电商模式全面解析
2019/08/01 职场文书
MySQL中distinct与group by之间的性能进行比较
2021/05/26 MySQL