12306余票查询(高铁剩余票数查询)

快过年了,在外打工的游子们终于要踏上返乡的旅程了。尽管疫情严重,他们还是要回国。我们一年只能在远离家乡的时候回去一两次,所以想家!反正开12306买票。我一看,

快过年了,在外打工的游子们终于要踏上返乡的旅程了。尽管疫情严重,他们还是要回国。我们一年只能在远离家乡的时候回去一两次,所以想家!反正开12306买票。我一看,没有票。我们买不起这个头等舱的商务座。我们能做什么?

12306余票查询(高铁剩余票数查询)插图

如果坐不到直达航班,不如我们多花点钱买多站,或者买票先上车再补票?

这是个好主意。然后我要先看看每趟列车的所有站点,然后我会搜索车票。以G3136总线为例。第一站是宁波,最后一站是太原南。我只需要买宁波到杭州东之间的任意一站,最后一站是杭州东到太原南之间,就可以上车了。一想到要回家,我还是有点激动。

12306余票查询(高铁剩余票数查询)插图(1)

我一搜索,就一头雾水。这么多站名、始发站、到站的排列组合没有爆炸?简直要了我的命。回家容易吗?

不,我们在做什么,修改代码?给它这么多重复性的工作岂不是很快?去做吧。

此处省略一万字。。。

终于实现了!!!

只要输入始发地、目的地、发车时间,就可以输出所有的车次信息。有些火车可以直接买,有些不能。但是,备选方案都给了,只要选择其中一个,就可以买票回家。

12306余票查询(高铁剩余票数查询)插图(2)

代码如下:

# coding = utf-8 import requests import URL lib . parse as parse import TIME import JSON import pretty _ errors import ref om fake _ user agent import UserAgentTRAIN _ N number = 2 train = 3 departure _ STATION = 6 terminus = 7 departure _ TIME = 8 arrival _ TIME = 9 duration = 10IF _ BOOK = 11 date = 13 no _ SEAT = 29 hard _ SEAT = 28 soft _ SEAT = 27 def Citys():& # 34;""城市简称:返回:& # 34;""headers = { & # 39用户代理& # 39;:str(UserAgent()。random)} URL = & # 39;https://kyfw . 12306 . cn/OTN/resources/js/framework/station _ name . js?station _ version = 1.9141 & # 39content = requests.get(url=url,headers = headers)content = content . content . decode(& # 39;utf-8 & # 39;)content = content[content . find(& # 39;=')+2:-2]content _ list = content . split(& # 39;@')dict _ city = { } for city in content _ list:str _ 1 = city[city . find(& # 39;|')+1:]city _ name = str _ 1[str _ 1 . find(& # 39;|')+1:str _ 1 . find(& # 39;|')+4]# city _ name _ 1的简称= str _ 1[:str _ 1 . find(& # 39;|')]# city name dict _ city[city _ name _ 1]= city _ name return dict _ city def time():& # 34;""获取当前时间:return:& # 34;""list _ time = list(time . local time())year = str(list _ time[0])month = str(list _ time[1])day = str(list _ time[2])if len(month)= = 1:month = & # 39;0'+月if len(day)= = 1:day = & # 39;0'+ day return年、月、日dayproxy = { & # 39http & # 39: '122.226.57.70:8888'}class Train: def __init__(self,from_station,to_station,Train _ date = Time()[0]+& # 39;-'+Time()[1]+& # 39;-'+Time()[2]):self . from _ station = from _ station self . to _ station = to _ station self . train _ date = train _ date self . URL = & # 39;https://kyfw.12306.cn/otn/leftTicket/queryA?离开了。% s & leftTicketDTO。% s & leftTicketDTO。% s & purpose _ codes =成人& # 39;# self.headers = { # & # 39饼干& # 39;: 'JSESSIONID = 073 CAA 21150 f 40 aa 7 f 05551 f 7 E1 b 5 f 5 c;RAIL _ device id = stubhcdgh 45k 8 stskjvyk _ V6 qfniudpyzg 1 o 9 l 7 iymooppieeubeqcbruuv 0 wokjrv 4 mdx gi 08t 0 azwlq 4d 4 gu 6 ltnlh 7 emo 8 tdg wze 2 WP 3 ou a9 wiki p5ly-a3o-f 5 uhgmyx 8 ylecv 0 nzqdsl 9 grkjhrja 4 syw;RAIL _ EXPIRATION = 1642644949013guidesStatus = offhighContrastMode = defaltModecursorStatus = offBIGipServerpool _ index = 804258314.43286 . 0000;route = 6 f 50 b 51 FAA 11 b 987 e 576 CDB 301 e 545 c 4;bigipserveroton = 384827914.24610 . 0000 & # 39;} self.headers = { & # 39用户代理& # 39;:str(UserAgent()。random)} self . session = requests . session()self . session . get(& # 39;https://kyfw.12306.cn/otn/leftTicket/init? link typeid = DC & fs = % E6 % 9D % AD % E5 % B7 % 9E % E4 % B8 % 9C,HGH & ts = % E5 % A4 % AA % E5 % 8E % 9F % E5 % 8D % 97,TNV&date=2022-01-19&flag=N,N,Y & # 39,headers=self.headers,proxies = proxy,timeout=5) def station(self,train _ number):& # 34;""找到火车的起点和终点:返回:& # 34;""url = f & # 39https://kyfw.12306.cn/otn/czxx/queryByTrainNo? & # 39;\ f & # 39{ parse . urlencode({ & # 34;火车号& # 34;:train _ number })} & & # 39;\ f & # 39{ parse . urlencode({ & # 34;from _ station _ telecode & # 34:Citys()[self . from _ station]})} & & # 39;\ f & # 39{ parse . urlencode({ & # 34;to _ station _ telecode & # 34:Citys()[self . to _ station]})} & & # 39;\ f & # 39{ parse . urlencode({ & # 34;出发日期& # 34;:self . train _ date })} & # 39;self . headers[& # 39;用户代理& # 39;] = str(UserAgent()。random)content = self . session . get(URL,headers=self.headers,proxy = proxy,time out = 5)# content = requests . get(URL,headers=self.headers,proxy = proxy,time out = 5)content = content . content . decode(& # 39;utf-8 & # 39;)data = json.loads(内容)stations _ data = data[& # 39;数据& # 39;]['数据& # 39;]stations _ data . sort(key = lambda x:x[& # 39;站号& # 39;])from _ station _ idx = int(list(filter(lambda x:self . from _ station in x[& # 39;station _ name & # 39],stations _ data))[0][& # 39;站号& # 39;])from _ station _ buy =[station[& # 39;station _ name & # 39]for stations _ data[:from _ station _ idx]]to _ station _ buy =[station[& # 39;station _ name & # 39]for stations _ data[from _ station _ idx:]]return from _ station _ buy,to _ station _ buy def train(self):& # 34;""获取信息:返回:& # 34;""URL = self . URL %(parse . urlencode({ & # 34;train _ date & # 34:self.train_date})、parse . urlencode({ & # 34;from _ station & # 34:Citys()[self.from_station]})、parse . urlencode({ & # 34;to _ station & # 34:Citys()[self . to _ station]}))self . headers[& # 39;用户代理& # 39;] = str(UserAgent()。random)content = self . session . get(URL,headers=self.headers,proxy = proxy,time out = 5)# content = requests . get(URL,headers=self.headers,proxy = proxy,time out = 5)content = content . content . decode(& # 39;utf-8 & # 39;)data = JSON . loads(content)dict _ train = data[& # 39;数据& # 39;]['结果& # 39;]dict _ map = data[& # 39;数据& # 39;]['地图& # 39;]RES =[]for train in dict _ train:train _ split = train . split(& # 39;|')from_station_buy,to _ station _ buy = self . station(TRAIN _ split[TRAIN _ NUMBER])buy =[]for from _ station,to_station in [[x,y]for x in from _ station _ buy for y in to _ station _ buy]:IF TRAIN _ split[IF _ BOOK]= = & # 39;N & # 39以及self.book_if(from_station,to_station,TRAIN _ split[TRAIN]):buy . append(f & # 39;{ from _ station }-{ to _ station } & # 39;)TRAIN _ str =[TRAIN _ split[火车],dict _ map[TRAIN _ split[出发_车站]],dict _ map[TRAIN _ split[终点站]],TRAIN _ split[出发_时间],TRAIN _ split[到达_时间],TRAIN _ split[持续时间],& # 39;是的& # 39;IF train _ split[IF _ BOOK]= = & # 39;Y & # 39else & # 39不& # 39;, ', '。join(buy)]RES . append(& # 39;| '+ '| '。join(train _ str)+& # 39;|')return res def book_if(self,from_station,to_station,train _ number):& # 34;""检查是否有票:param from _ station::param to _ station::param train _ number::return:& # 34;""URL = self . URL %(parse . urlencode({ & # 34;train _ date & # 34:self.train_date})、parse . urlencode({ & # 34;from _ station & # 34:Citys()[from_station]})、parse . urlencode({ & # 34;to _ station & # 34:Citys()[to _ station]}))self . headers[& # 39;用户代理& # 39;] = str(UserAgent()。random)content = self . session . get(URL,headers=self.headers,proxy = proxy,time out = 5)# content = requests . get(URL,headers=self.headers,proxy = proxy,time out = 5)content = content . content . decode(& # 39;utf-8 & # 39;)data = JSON . loads(content)dict _ train = data[& # 39;数据& # 39;]['结果& # 39;]train = list(filter(lambda x:x . split(& # 39;|')[TRAIN] == train_number,dict_train))如果不是TRAIN:返回如果train[0]则返回True。拆分(& # 39;|')[IF _ BOOK]= = & # 39;Y & # 39else Falseif _ _ name _ _ = = & # 39_ _ main _ _ & # 39:打印(& # 39;-12306信息查询-)while True:from _ station = input(& # 39;请输入原点:& # 39;)或者& # 39;杭州& # 39;if from _ station in cities():break while True:to _ station = input(& # 39;请输入目的地:& # 39;)或者& # 39;太原& # 39;if to _ station in Citys():break pattern = re . compile(& # 39;\ d { 4 }-\ d { 2 }-\ d { 2 } & # 39;)而True:date = input(& # 39;请输入出发时间(注意格式:2022-02-01,默认为购票当天):& # 39;)if not date或re.match(pattern,date):break if not date:date = Time()[0]+& # 39;-'+Time()[1]+& # 39;-'+Time()[2]Train = Train(from _ station,to_station,date)# information = Train . Train()print(& # 39;- ')打印(& # 39;-)打印(& # 39;- ')打印(& # 39;|车次|始发站|到达站|出发时间|到达时间|时长|直购|备选| & # 39;)for info in information:print(info)print(& # 39;- ')这是第一版的实现。目前只是一个粗略的替代方案,节省了人们手动搜索的时间。还有很多功能没有实现。

没有备选排名:虽然给出了备选,但哪个备选好一点没有给个排序没有座位信息(商务/一等/二等/硬座/无座):虽然能买,但是不一定能买到适合自己的(便宜的),有点奢侈了

回家很开心,准备实现二版了。...

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。

作者:美站资讯,如若转载,请注明出处:https://www.meizw.com/n/106355.html

发表回复

登录后才能评论