--------blogs:  陈月白    http://www.cnblogs.com/chenyuebai    --------

一、概述

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

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

大概80行代码,主要是:

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

python+tkinter实现界面,详见之前的学习笔记:http://www.cnblogs.com/chenyuebai/p/7150382.html

最终效果图:

二、实现

1.界面读取待查询车次

之前总结过使用tkinter实现GUI,详见之前的笔记:http://www.cnblogs.com/chenyuebai/p/7150382.html

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)
                    ":
                        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:获取火车至南京信息完成")

完成。最终感叹,调接口确实比自己爬省事多了哈哈哈!  ヾ(◍°∇°◍)ノ゙

END

2017.12.20

交作业了,妹子很喜欢!nice!

python 3.6 tkinter+urllib+json 火车车次信息查询的更多相关文章

  1. Python大法之从火车余票查询到打造抢Supreme神器

    本文作者:i春秋作家——阿甫哥哥 系列文章专辑:https://bbs.ichunqiu.com/forum.php?mod=collection&action=view&ctid=9 ...

  2. 火车车次查询-余票查询--Api接口

    1.来自12306的火车车次数据 使用12306网站的接口,查询余票.此接口采集自 这里. 全国火车站代号字典,下载 . 火车票余票查询 http://dynamic.12306.cn/otsquer ...

  3. python的httplib、urllib和urllib2的区别及用

    慢慢的把它们总结一下,总结就是最好的学习方法 宗述 首先来看一下他们的区别 urllib和urllib2 urllib 和urllib2都是接受URL请求的相关模块,但是urllib2可以接受一个Re ...

  4. java实现根据起点终点和日期查询去哪儿网的火车车次和火车站点信息

    本文章为原创文章,转载请注明,欢迎评论和改正. 一,分析 之前所用的直接通过HTML中的元素值来爬取一些网页上的数据,但是一些比较敏感的数据,很多正规网站都是通过json数据存储,这些数据通过HTML ...

  5. Python爬虫实践~BeautifulSoup+urllib+Flask实现静态网页的爬取

    爬取的网站类型: 论坛类网站类型 涉及主要的第三方模块: BeautifulSoup:解析.遍历页面 urllib:处理URL请求 Flask:简易的WEB框架 介绍: 本次主要使用urllib获取网 ...

  6. Python导出Excel为Lua/Json/Xml实例教程(三):终极需求

    相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 Python导出E ...

  7. Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验

    Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出E ...

  8. Python导出Excel为Lua/Json/Xml实例教程(一):初识Python

    Python导出Excel为Lua/Json/Xml实例教程(一):初识Python 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出 ...

  9. Python标准库之urllib,urllib2

    urllib模块提供了一些高级接口,用于编写需要与HTTP服务器交互的客户端.典型的应用程序包括从网页抓取数据.自动化.代理.网页爬虫等. 在Python 2中,urllib功能分散在几个不同的库模块 ...

随机推荐

  1. code forces 439 C. The Intriguing Obsession

    C. The Intriguing Obsession time limit per test 1 second memory limit per test 256 megabytes input s ...

  2. Problem A

    Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...

  3. Sudoku Killer

    Problem Description 自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会将数独列为一个单独的项目进行 ...

  4. 用git上传本地文件到github

    1.在自己的github账号下新建仓库--------得到github仓库地址 2.本地安装git---在将要克隆的文件夹下 右击点击Git Bash Here 3.输入命令 $ git clone ...

  5. Log4j – Log4j 2 API

    Overview The Log4j 2 API provides the interface that applications should code to and provides the ad ...

  6. Python pip 下载速度慢? Windows 设置 国内源,用 阿里云 国内镜像 加速

    pip 提供了对 Python 包的查找.下载.安装.卸载的功能,是非常方便的 Python 包管理工具.但是,令人苦恼的是 pip 在国内的下载速度非常慢,速度常常只有每秒几十 K,甚至才几 K,小 ...

  7. PHP读取数据库表显示到前台

    <?php$username=$_GET['uid']; //获取一个值作为查询条件 $result=$db->query("select * from trip where a ...

  8. JAVA 后台SSM框架接收安卓端的json数据

    最近项目上与安卓端做JSON数据交互,使用的SSM框架,刚开始的时候感觉很简单,想着不就是把安卓端的JSON数据封装为Bean类对象吗? 于是就这样写了 可是这样一直报400,百度原因是因为请求url ...

  9. 树莓派配置允许WINDOWS远程桌面 x11nvc+xrdp

    20171109 网上很多设置教程都比较老旧,于是自己整理一下顺便分享下 开启SSH后,使用PUTTY连接. 安装x11vnc sudo apt-get install x11vnc 设置密码 sud ...

  10. 大话git中的撤销操作

    下面以现实场景作为情境. 基础知识,理解git中的几个区域 本地代码已经add,未commit 修改本地工作目录中的readme.md,添加文字"第一次修改" 然后查看下状态 ➜ ...