前言

今天看到一位朋友发了朋友圈,只有出发站和票价,以及车次的前三位K11,没有终点站,问我们这是去哪里的;我就去网上查了一下出发站上面以K11开头的车次,然后每一个站每一站地去查了一下票价;最后发现是从唐河->南京,K1108次,卧铺车。他表示很惊讶,你怎么知道我是去南京的,心里暗自高兴。但是自己又在想着能不能把12306上面的车站和车次,票价都给弄下来呢?

接口分析

车次查询

https://kyfw.12306.cn/otn/czxx/queryByTrainNo?train_no=330000K65505&from_station_telecode=THF&to_station_telecode=TBF&depart_date=2018-04-13
{
  "data": {
    "data": [
      {
        "arrive_time": "----",
        "end_station_name": "北京",
        "isEnabled": true,
        "service_type": "2",
        "start_station_name": "乌鲁木齐",
        "start_time": "18:23",
        "station_name": "乌鲁木齐",
        "station_no": "01",
        "station_train_code": "Z180",
        "stopover_time": "----",
        "train_class_name": "直特"
      }.....
      {
        "arrive_time": "09:45",
        "isEnabled": true,
        "start_time": "09:45",
        "station_name": "北京",
        "station_no": "20",
        "stopover_time": "----"
      }
    ]
  },
  "httpstatus": 200,
  "messages": [],
  "status": true,
  "validateMessages": {},
  "validateMessagesShowId": "_validatorMessage"
}

票价查询

https://kyfw.12306.cn/otn/leftTicket/queryTicketPrice?train_no=400000K42803&from_station_no=08&to_station_no=09&seat_types=1413&train_date=2018-04-14
{
  "data": {
    "1": "125",
    "3": "585",
    "4": "875",
    "A1": "¥12.5",
    "A3": "¥58.5",
    "A4": "¥87.5",
    "OT": [],
    "WZ": "¥12.5",
    "train_no": "400000K42803"
  },
  "httpstatus": 200,
  "messages": [],
  "status": true,
  "validateMessages": {},
  "validateMessagesShowId": "_validatorMessage"
}
  • A1:YZ
  • A3:YW
  • A4:RW
  • A6:高级软卧
  • A9:商务特等座
  • M:一等座
  • 0:二等座
  • F:动卧
  • WZ:WZ

刷新验证码图片

https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=other&rand=sjrand

问题列表

  • 车次有编码和编号,如何获取对应的关系;在resource里面有一个train_list.js配置文件,专门存放车次别名,统计出来有13342个车次
var train_list ={"2018-04-19":{"station_train_code":"D45(北京-大连北)","train_no":"2400000D450F"}}
  • 车站也有编码和编号,如何获取对应的关系;在resource里面有一个js配置文件,专门存放站别名;统计出来有2726个站点
var station_names ='@bjb|北京北|VAP|beijingbei|bjb|0@bjd|北京东|BOP|beijingdong|bjd|1@bji|北京|BJP|beijing|bj|2@bjn|北京南|VNP|beijingnan|bjn|3@bjx|北京西|BXP|beijingxi|bjx|4@gzn|.....