12306购票解析车站信息以及车辆信息

Posted ZSYL

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了12306购票解析车站信息以及车辆信息相关的知识,希望对你有一定的参考价值。

  1. 了解 解析车站信息
  2. 了解 解析车辆信息

1. 解析城市/车站编码

城市/车站编码js文件有版本号

# 12306.funk12306.get_stations_dict
import re
import json
import requests

# 获取车站编号字符串 station_version=1.9076
url = 'https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.9076'
resp = requests.get(url)
stations_str = re.search("'(.*)'", resp.text).group(1)

# 获取{城市(车站):编码, ...} 键值对
stations_dict = {}
for station in stations_str.split('@'):
    if station == '': # 按@切会切出空字符串
        continue
    stations_dict[station.split('|')[1]] = station.split('|')[2]

with open('./utils/stations_dict.py', 'w', encoding='utf8') as f:
    f.write('stations_dict = ')
    json.dump(stations_dict, f, ensure_ascii=False, indent=4)

2. 获取车辆信息

# 12306.funk12306.Funk12306.buy_ticket

    def buy_ticket(self):
        self.s.headers.pop('Referer') # 清除referer

        # 获取城市(车站)编码
        from_station = input('输入出发城市或车站:')
        to_station = input('输入到达城市或车站:')
        train_date = input('输入出行日期,格式为2018-12-03:')
        from_station_code = stations_dict.get(from_station, '')
        to_station_code = stations_dict.get(to_station, '')

        # 查询车量信息日志get
        url = 'https://kyfw.12306.cn/otn/leftTicket/log?leftTicketDTO.train_date=%s&leftTicketDTO.from_station=%s&leftTicketDTO.to_station=%s&purpose_codes=ADULT' % (train_date, from_station_code, to_station_code)
        resp = self.s.get(url)
        print(resp.text)
        # 查询车量具体信息query
        url = 'https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=%s&leftTicketDTO.from_station=%s&leftTicketDTO.to_station=%s&purpose_codes=ADULT' % (train_date, from_station_code, to_station_code)
        response = self.s.get(url)
        # 打印trains_list
        print(json.loads(response.content)['data']['result'])

3. 解析车辆信息

# 12306.utils.parse_trains_infos

import urllib.parse

def parseTrainsInfos(trains_list):
    """
    解析列车信息列表, 返回列车信息列表
    """
    trains_infos_list = []

    if trains_list == []:
        return []

    for train_info in trains_list:
        train_info_list = train_info.split('|')
        train_info_dict = {}
        # 构造列车信息
        train_info_dict['secretStr'] = urllib.parse.unquote(train_info_list[0])  # secretStr ;为''时无法购买车票
        # train_info_list[1]  预定/列车停运
        train_info_dict['train_no'] = urllib.parse.unquote(train_info_list[2])  # train_no
        train_info_dict['stationTrainCode'] = urllib.parse.unquote(train_info_list[3])  # stationTrainCode 即车次 # 展示
        train_info_dict['start_station'] = urllib.parse.unquote(train_info_list[4])  # 始发站 # 展示
        train_info_dict['end_station'] = urllib.parse.unquote(train_info_list[5])  # 终点站 # 展示
        train_info_dict['from_station'] = urllib.parse.unquote(train_info_list[6])  # 出发站 # 展示
        train_info_dict['to_station'] = urllib.parse.unquote(train_info_list[7])  # 到达站 # 展示
        train_info_dict['from_time'] = urllib.parse.unquote(train_info_list[8])  # 出发时间 # 展示
        train_info_dict['to_time'] = urllib.parse.unquote(train_info_list[9])  # 到达时间 # 展示
        train_info_dict['use_time'] = urllib.parse.unquote(train_info_list[10])  # 时长 # 展示
        train_info_dict['buy_able'] = urllib.parse.unquote(train_info_list[11])  # 能否购买 Y 可以购买 N 不可以购买 IS_TIME_NOT_BUY 停运 # 展示
        train_info_dict['leftTicket'] = urllib.parse.unquote(train_info_list[12])  # leftTicket
        train_info_dict['start_time'] = urllib.parse.unquote(train_info_list[13])  # 车次始发日期 # 展示
        train_info_dict['train_location'] = urllib.parse.unquote(train_info_list[15])  # train_location 不知道是啥??
        train_info_dict['from_station_no'] = urllib.parse.unquote(train_info_list[16])  # 出发站编号
        train_info_dict['to_station_no'] = urllib.parse.unquote(train_info_list[17])  # 到达站编号
        # 14,18,19,20,27,34,35未知
        train_info_dict['gaojiruanwo'] = urllib.parse.unquote(train_info_list[21])  # 高级软卧 # 展示
        train_info_dict['qita'] = urllib.parse.unquote(train_info_list[22])  # 其他 # 展示
        train_info_dict['ruanwo'] = urllib.parse.unquote(train_info_list[23])  # 软卧 # 展示
        train_info_dict['ruanzuo'] = urllib.parse.unquote(train_info_list[24])  # 软座 # 展示
        train_info_dict['tedengzuo'] = urllib.parse.unquote(train_info_list[25])  # 特等座 # 展示
        train_info_dict['wuzuo'] = urllib.parse.unquote(train_info_list[26])  # 无座 # 展示
        train_info_dict['yingwo'] = urllib.parse.unquote(train_info_list[28])  # 硬卧 # 展示
        train_info_dict['yingzuo'] = urllib.parse.unquote(train_info_list[29])  # 硬座 # 展示
        train_info_dict['erdengzuo'] = urllib.parse.unquote(train_info_list[30])  # 二等座 # 展示
        train_info_dict['yidengzuo'] = urllib.parse.unquote(train_info_list[31])  # 一等座 # 展示
        train_info_dict['shangwuzuo'] = urllib.parse.unquote(train_info_list[32])  # 商务座 # 展示
        train_info_dict['dongwo'] = urllib.parse.unquote(train_info_list[33])  # 动卧 # 展示
        trains_infos_list.append(train_info_dict)
    return trains_infos_list

4. 获取后续请求所需的参数

# 12306.funk12306.Funk12306.buy_ticket  

        # 解析获取trains_list
        trains_list = parseTrainsInfos(json.loads(response.content)['data']['result'])
        print('查询的列车信息如下:')
        pprint(trains_list)
        # 获取选择的列车
        train_info_dict = trains_list[int(input('请输入选中车次的下标:'))]
        print('选中了列车信息为:')
        pprint(train_info_dict)
        # 列车信息
        secretStr = train_info_dict['secretStr']
        leftTicket = train_info_dict['leftTicket']
        train_location = train_info_dict['train_location']

小结

  1. 了解 解析车站信息
  2. 了解 解析车辆信息

以上是关于12306购票解析车站信息以及车辆信息的主要内容,如果未能解决你的问题,请参考以下文章

12306密码格式?

12306购票测试运行以及完整代码

12306购票构造时间参数以及下单购票

爬虫实战12306购票抓包分析以及任务分解

60万12306用户信息暗网遭售卖?官方:第三方泄露

C语言火车购票系统