侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

Python  /  管理员 发布于 7年前   182

一、概述

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

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

大概80行代码,主要是:

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

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

最终效果图:

二、实现

1.界面读取待查询车次

之前总结过使用tkinter实现GUI,详见之前的笔记:///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如何生成hash值示例详解
    下一条:
    python实现神经网络感知器算法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客