|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 Excel实战分享 于 2018-1-14 12:36 编辑
春运将至,很多小伙伴都在刷火车票吧,希望大伙都能抢到票,早日平安回家。
利用Power Query查询火车票余票信息,来凑个热闹,先上图。
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
其中步骤大概分为3步:
1、获取站名代号与站名对应表
2、再根据传递进来的参数,获取相关余票原始数据
3、对余票原始数据进行数据处理,最终以表格的形式呈现出来
- let
- 出发地="北京",
- 目的地="上海",
- 出发日="2018-02-11",
- url="https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.9027", //车站信息链接
- web_stations = Text.BetweenDelimiters(Text.FromBinary(Binary.Buffer(Web.Contents(url))),"var station_names ='","';"), //车站信息
- sta=(n as number) as list=>List.Buffer(List.Alternate(List.Skip(Text.Split(web_stations,"|"),n),4,1,1)), //用于获取车站名称列表、车站代号列表
- stations=(s as text) as text=>sta(2){List.PositionOf(sta(1),s)}, //获取指定车站名称的车站代号
- url2="https://kyfw.12306.cn/otn/leftTicket/queryZ", //余票查询链接
- headers=[#"User-Agent"="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",#"X-Requested-With"="XMLHttpRequest"],
- query=[leftTicketDTO.train_date=出发日,leftTicketDTO.from_station=stations(出发地),leftTicketDTO.to_station=stations(目的地),purpose_codes="ADULT"],
- web_data=Json.Document(Binary.Buffer(Web.Contents(url2,[Query=query,Headers=headers])))[data], //车次信息
- fields={"车次","出发站","到达站","出发时间","到达时间","历时","商务座","一等座","二等座","高级软卧","软卧","动卧","硬卧","软座","硬座","无座","其他"},
- business=(a as text,b as text) as text=>if a&b="" then "" else if a&b="无" or a&b="无无" then "无" else if a<>"无" and a<>"" then a else b, //特等座与商务座的余票信息
- seat=(lst as list,i as number) as text=>if i=6 or i=7 then "【"&{"过","始","过","终"}{Number.From(lst{i}=lst{i-2})+i*2-12}&"】 "&Record.Field(web_data[map],lst{i})
- else if i=25 then business(lst{25},lst{32})
- else lst{i}, //指定车次、指定座位的余票信息
- records=(lst as list) as list=>List.Transform({3,6,7,8,9,10,25,31,30,21,23,33,28,24,26,29,27},each seat(lst,_)), //指定车次的余票信息
- standby = #table(fields,List.Transform(web_data[result],each records(Text.Split(_,"|")))), //余票信息
- final=Table.Buffer(Table.ReplaceValue(standby,"","--",(x,y,z)=>if x=y then z else x,fields)) //表格整理
- in
- final
复制代码
|
评分
-
7
查看全部评分
-
|