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的Django框架测试驱动开发的教程
Apr 22 Python
python使用线程封装的一个简单定时器类实例
May 16 Python
Python实现PS滤镜Fish lens图像扭曲效果示例
Jan 29 Python
Python中的random.uniform()函数教程与实例解析
Mar 02 Python
python os模块简单应用示例
May 23 Python
解析Python3中的Import
Oct 13 Python
python3 pillow模块实现简单验证码
Oct 31 Python
python各层级目录下import方法代码实例
Jan 20 Python
Django DRF路由与扩展功能的实现
Jun 03 Python
tensorflow之读取jpg图像长和宽实例
Jun 18 Python
Win10环境中如何实现python2和python3并存
Jul 20 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
JSON在PHP中的应用介绍
2012/09/08 PHP
php实现过滤UBB代码的类
2015/03/12 PHP
PHP date()格式MySQL中插入datetime方法
2019/01/29 PHP
PHP7引入的"??"和"?:"的区别讲解
2019/04/08 PHP
Laravel框架控制器的middleware中间件用法分析
2019/09/30 PHP
基于jquery的点击链接插入链接内容的代码
2012/07/31 Javascript
JavaScript异步回调的Promise模式封装实例
2014/06/07 Javascript
使用命令对象代替switch语句的写法示例
2015/02/28 Javascript
JavaScript中var关键字的使用详解
2015/08/14 Javascript
location.hash保存页面状态的技巧
2016/04/28 Javascript
Javascript之深入浅出prototype
2017/02/06 Javascript
vue实现样式之间的切换及vue动态样式的实现方法
2017/12/19 Javascript
vue+elementUI实现图片上传功能
2019/08/20 Javascript
Vue.js组件props数据验证实现详解
2019/10/19 Javascript
js 数据类型判断的方法
2020/12/03 Javascript
jquery实现鼠标悬浮弹出气泡提示框
2020/12/23 jQuery
Django1.3添加app提示模块不存在的解决方法
2014/08/26 Python
Python中的字典遍历备忘
2015/01/17 Python
python计算一个序列的平均值的方法
2015/07/11 Python
Python代码打开本地.mp4格式文件的方法
2019/01/03 Python
nginx搭建基于python的web环境的实现步骤
2020/01/03 Python
基于python求两个列表的并集.交集.差集
2020/02/10 Python
Python hashlib和hmac模块使用方法解析
2020/12/08 Python
selenium+headless chrome爬虫的实现示例
2021/01/08 Python
New Balance美国官网:运动鞋和健身服装
2017/04/11 全球购物
澳大利亚正品化妆品之家:Cosmetic Capital
2017/07/03 全球购物
SportsDirect.com马来西亚:英国第一体育零售商
2018/11/21 全球购物
新加坡第一的杂货零售商:NTUC FairPrice
2020/12/05 全球购物
优秀毕业生事迹材料
2014/02/12 职场文书
高中竞选班长演讲稿
2014/04/24 职场文书
小学趣味运动会加油稿
2014/09/25 职场文书
2014年大学班长工作总结
2014/11/14 职场文书
2014年行政后勤工作总结
2014/12/06 职场文书
逃课检讨书怎么写
2015/01/01 职场文书
2015选调生工作总结
2015/07/24 职场文书
laravel添加角色和模糊搜索功能的实现代码
2021/06/22 PHP