全スタックエンジニアの道--19日目

4221 ワード

ヘビー級インタフェース!!12306汽車の切符のインタフェースの使用
列車の切符のインタフェースの使用は難しくありません最も重要なのは1つの要求のURLのつなぎ合わせで、URLの住所は以下の通りです:
var url = '/otn/leftTicket/query?leftTicketDTO.train_date='+req.body.date+'&leftTicketDTO.from_station='+req.body.start+'&leftTicketDTO.to_station='+req.body.end+'&purpose_codes=ADULT';

次のようなパラメータも必要です.
var options = {
    hostname: 'kyfw.12306.cn',
    port: 443,
    path: url,
    method: 'GET',
    rejectUnauthorized: false
};

その後、シミュレーションがリクエストを送信すると、大量のデータが返されます.JSON返却例:
{
"error_code": "0",
"reason": "Success",
"result": [{
    "train_no": "D2287",
    /*  */
    "start_station_name": "    ",
    /*     */
    "end_station_name": "   ",
    /*     */
    "from_station_name": "    ",
    /*   */
    "to_station_name": "   ",
    /*   */
    "start_time": "06:25",
    /*    */
    "arrive_time": "10:53",
    /*    */
    "train_class_name": "  ",
    /*    */
    "day_difference": "0",
    /*    */
    "lishi": "04:28",
    /*     */
    "gr_num": "--",
    /*    :--       */
    "qt_num": "--",
    /*  */
    "rw_num": "--",
    /*  */
    "rz_num": "--",
    /*  */
    "tz_num": "--",
    /*   */
    "wz_num": " ",
    /*  */
    "yw_num": "--",
    /*  */
    "yz_num": "--",
    /*  */
    "ze_num": " ",
    /*   */
    "zy_num": " ",
    /*   */
    "swz_num": "--"
    /*   */
},
{
    "train_no": "D3203",
    "start_station_name": "    ",
    "end_station_name": "   ",
    "from_station_name": "    ",
    "to_station_name": "   ",
    "start_time": "06:30",
    "arrive_time": "11:09",
    "train_class_name": "  ",
    "day_difference": "0",
    "lishi": "04:39",
    "gr_num": "--",
    "qt_num": "--",
    "rw_num": "--",
    "rz_num": "--",
    "tz_num": "--",
    "wz_num": " ",
    "yw_num": "--",
    "yz_num": "--",
    "ze_num": " ",
    "zy_num": " ",
    "swz_num": "--",
    "ticketprice": {
        "OT": [],
        "WZ": "¥226.0",
        "M": "¥335.5",
        "A9": "¥689.0",
        "9": "6890",
        "O": "¥226.0",
        "train_no": "5l000G753722"
    }
},
{
    "train_no": "G7501",
    "start_station_name": "    ",
    "end_station_name": "  ",
    "from_station_name": "    ",
    "to_station_name": "   ",
    "start_time": "07:00",
    "arrive_time": "11:00",
    "train_class_name": "",
    "day_difference": "0",
    "lishi": "04:00",
    "gr_num": "--",
    "qt_num": "--",
    "rw_num": "--",
    "rz_num": "--",
    "tz_num": "--",
    "wz_num": "165",
    "yw_num": "--",
    "yz_num": "--",
    "ze_num": " ",
    "zy_num": " ",
    "swz_num": "15",
    "ticketprice": {
        "OT": [],
        "WZ": "¥226.0",
        "M": "¥335.5",
        "A9": "¥689.0",
        "9": "6890",
        "O": "¥226.0",
        "train_no": "5l000G753722"
    }
  }]
 }

サイト間では中国語ではなくサイトコードで表示する必要があることに注意してください.そのため、ネット上でコード対応のサイト情報を探す必要があります.