Python 3.6 Tkinter+urllib+json Train trips information query

Source: Internet
Author: User

--------Blogs: Chen http://www.cnblogs.com/chenyuebai --------

I. Overview

When she works, she needs to inquire a lot of information about trains to Nanjing , including the arrival station ( Nanjing Station or Nanjing South Railway station), arrival time, departure time and so on, then make the next work according to these information.

Version of the end, take advantage of the intermittent period, help her to get a simple batch query tool, the pink button is for her to use ~ haha ha! (?*?*?)

About 80 lines of code, mainly:

Interface reading to be queried-----Call train information interface----parse return data----assembly result----Package to Interface (Tkinter)

Python+tkinter implementation interface, see previous study notes: http://www.cnblogs.com/chenyuebai/p/7150382.html

Eventually:

Second, the realization

1. Interface read the train to be queried

Previously summarized using the Tkinter implementation GUI, see previous notes :http://www.cnblogs.com/chenyuebai/p/7150382.html

2. call the train information interface

Off-topic, before is done from the interface to read the train information to be queried, and then constructed into Ctrip's query URL, take the data after filtering information;

But after fetching the page data, decode found the total parabolic decoding exception, Baidu, the reason is the page source code format has a variety, decode need to add a wrong skip parameters.

But the train information happens to skip the list ...

But has already told the sister to be able to do well soon (Pack B), therefore directly applies a third party platform the interface Qaq

Online check, the free interface basically do not provide services. So the use of a third-party platform interface (a speed data), the registration of 1000, the renewal fee of 5 yuan 1W (temporarily not continued =. =

    #Call train information interface, get train information    defGettrainscheduleinfo (self,trainschedule): Trainbaseinfo=""        #Stitching URLURL ="http://api.xxxx.com/train/line?appkey=xxxxxx&trainno="+Trainschedule#print (URL)        #Get Data        Try: Trainbaseinfo=self.send_get_request (URL) #发送GET请求, Python3. X is used Urllib.request library, many online         except:            Print("error:func gettrainscheduleinfo Select_items_from_url failed.url =%s, flag =%s"%(trainschedule))returnTrainbaseinfo

3. Parse the return data

The return data is a JSON type of string, directly json.loads, parsing can

#get all the information you want to checkAlltrainresultdic = {}#Train Query Result collection         forTrainscheduleinchTrainschedulelist:trainbaseinfo= Self.gettrainscheduleinfo (Trainschedule)#JSON string            ##----Test----            # Trainbaseinfo = "{" "status": "0", "msg": "OK", "result": {"Trainno": "G8", "type": "High Speed rail", "list": [{"Sequenceno": "1", " Station ":" Shanghai Hongqiao "," 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 ":" Nanjing South " , "Day": "1", "ArrivalTime": "20:00", "Departuretime": "20:02", "StopTime": "2", "Costtime": "$", "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 ":" Jinan West "," 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 ":" Tianjin South "," 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 ":" Beijing South "," 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 "}]}}"            ##----Test----            Print("Trainbaseinfo =", Trainbaseinfo)#parsing            ifTrainbaseinfo:Try: Trainbaseinfo_loads=json.loads (trainbaseinfo)iftrainbaseinfo_loads["Status"] =="0": Resultnodevalue= trainbaseinfo_loads["result"] Trainnonodevalue= resultnodevalue["Trainno"]#Query the train codeTypenodevalue = resultnodevalue["type"]#type of TrainListnodevalue = resultnodevalue["List"]#Path Site information Collection List                        #To sift through Nanjing, Nanjing South                         forTraininfoinchListnodevalue:if(cityName1inchTraininfo.values ())or(cityName2inchtraininfo.values ()):#parsing DataArrivedstation = traininfo[" Station"]#Arrival StationArrivedtime = traininfo["ArrivalTime"]#Arrival TimeLeavetime = traininfo["Departuretime"]#off-site time                                ifArrivedstation = ="Nanjing": Arrivedstation="Nanjing Station"                                      #Store the results of this train queryTrainresult =[] Trainresult.append (arrivedstation) Trainresult.appen D (arrivedtime) trainresult.append (leavetime) Trainresult.app End (Typenodevalue) Alltrainresultdic[trainschedule]=TrainresultElse:                                #Self.write_log_to_text ("ERROR: n°:%s No way Nanjing station information, skip"% trainschedule)                                Continue                    Else: Self.write_log_to_text ("ERROR: n°:%s Check the return data status code is not 0, skip"%trainschedule)Continue                except: Self.write_log_to_text ("ERROR: n°:%s returned JSON string failed"%trainschedule)Else: Self.write_log_to_text ("ERROR: n°:%s Query interface return information is empty, skipped"%trainschedule)Continue        Print(Alltrainresultdic)

4. Assembly results, interface output

        #assembly result Interface OutputSelf.result_data_Text.delete (1.0, END) head="train station arrival time departure Time type"Self.result_data_Text.insert (1.0, head) forTraininchAlltrainresultdic.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: Get train to Nanjing information complete")

Complete.  Finally sigh, tune the interface is indeed more than their own crawl more convenient haha ha! ヾ (? °?°?)??

END

2017.12.20

Hand in the homework, sister very much! nice!

Python 3.6 Tkinter+urllib+json Train trips information query

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.