Requests grab fire ticket data

Source: Internet
Author: User
Tags ticket

1. Data interface

https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=adult&querydate=2016-08-01&from_station=ncg& To_station=czq

Returned is the 2015-8-01 Nanchang to Chenzhou train ticket information, in the form of JSON.

The data looks like this:

{    "Validatemessagesshowid":"_validatormessage",    "Status": true,"Httpstatus": 200,    "Data":{            "datas":[                        {                             "Train_no":"5u000g140101",                             "Station_train_code":"G1401",                             "Start_station_telecode":"NXG",                             "Start_station_name":"Nanchang West",                             "End_station_telecode":"IZQ",                             "End_station_name":"Guangzhou South",                             "From_station_telecode":"NXG",                             "From_station_name":"Nanchang West",                             "To_station_telecode":"ICQ",                             "To_station_name":"Chenzhou West",                             "start_time":"07:29",                             "Arrive_time":"10:42",                             "day_difference":"0",                             ...                             "Swz_num":"No"                        },                        {                              ...                        } ]}

2. Get GZ to Shenzhen train ticket data

ImportRequestsImportJSONclassTrainticketssprider:defGetticketsinfo (self,purpose_codes,querydate,from_station,to_station): Self.url='https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=%s&queryDate=%s&from_station=%s&to_ station=%s'%(purpose_codes,querydate,from_station,to_station) self.headers= {                    "Accept":"Text/html,application/xhtml+xml,application/xml;",                    "accept-encoding":"gzip",                    "Accept-language":"zh-cn,zh;q=0.8",                    "user-agent":"mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/42.0.2311.90 safari/537.36"} self. Ticketsession=requests. Session () self. Ticketsession.verify= False#Turn off HTTPS authenticationSelf. Ticketsession.headers =self.headersTry: Self.resp_json=Self . Ticketsession.get (self.url) Self.ticketsdatas= Json.loads (Self.resp_json.text) ["Data"]["datas"]            returnSelf.ticketsdatasexceptException as E:Print(e)defIszero (num):ifnum = ='--' or 'No':        return '0'    Else:        returnNumdefMain (): Purpose_codes='Adult'querydate='2016-08-01'from_station='Gzq'to_station='Szq'Ticketsprider=Trainticketssprider () Res=ticketsprider.getticketsinfo (purpose_codes,querydate,from_station,to_station) forI,ticketinfoinchEnumerate (res):Print(U"n°:%s"%ticketinfo["Station_train_code"])                Print(U"Start Station:%s"%ticketinfo["Start_station_name"])                Print(U"Destination:%s"%ticketinfo["To_station_name"])                Print(U"driving time:%s"%ticketinfo["start_time"])                Print(U"arrival time:%s"%ticketinfo["Arrive_time"])                Print(U"second-waiting seat left:%s ticket"% (ticketinfo["Ze_num"]))                Print(U"one seat left:%s ticket"% (ticketinfo["Zy_num"]))                Print(U"Business seatstill left:%s ticket"% (ticketinfo["Swz_num"]))Print(U"no seat left:%s ticket"% (ticketinfo["Wz_num"]))                Print(U"whether there is a ticket:%s"%ticketinfo["Canwebbuy"])                Print("**********************************")if __name__=='__main__': Main ()

The results are as follows:

N°: G6205
Starting Station: Guangzhou South
Destination: Shenzhen North
Driving time: 07:20
Arrival time: 07:57
Second-level seats left: 708 tickets
One seat left: 108 tickets
Business seats left: 24 tickets
There is no seat left: no ticket
Whether there is a ticket: Y

Requests fetching fire ticket data

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.