💻 源代码
# -*- coding: utf-8 -*-
import requests
def check_ticket(train_date, from_station, to_station):
"""查询12306余票"""
url = f'https://kyfw.12306.cn/otn/leftTicket/queryX?leftTicketDTO.train_date={train_date}&leftTicketDTO.from_station={from_station}&leftTicketDTO.to_station={to_station}&purpose_codes=ADULT'
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) Gecko/20100101 Firefox/61.0'
}
res = requests.get(url, headers=header)
res = res.json()
result = res['data']['result']
for i in result:
temp_list = i.split("|")
# 座位类型: 26=无座, 23=软座, 29=硬座, 28=硬卧
if temp_list[26] == "有":
print(f"无座有票: {temp_list[3]}")
if temp_list[23] == "有":
print(f"软座有票: {temp_list[3]}")
if temp_list[29] == "有":
print(f"硬座有票: {temp_list[3]}")
if __name__ == '__main__':
# 示例: 查询2024-01-15 长沙到北京
check_ticket('2024-01-15', 'CSQ', 'BJP')