炫意html5
最早CSS3和HTML5移动技术网站之一

关于如何获得12306的列车信息

先看代码

    data_t = requests.get('https://kyfw.12306.cn/otn/leftTicket/query?',headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36",
"Referer":'https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc'#https://kyfw.12306.cn/otn/leftTicket/init?
},params = add_url_queryZ_params())
print(data_t.json())

这是params
leftTicketDTO.train_date=2021-04-29&leftTicketDTO.from_station=BJP&leftTicketDTO.to_station=SHH&purpose_codes=ADULT

但是print的输出为空 即没有获取到 json文件 有大佬知道是怎么回事吗 多谢了

回答

加cookies

import requests

cookies = {
    '_jc_save_fromDate': '2021-05-01',    #开车的时间
}

headers = {
    'Connection': 'keep-alive',
    'Cache-Control': 'no-cache',
    'sec-ch-ua': '^\\^',
    'Accept': '*/*',
    'X-Requested-With': 'XMLHttpRequest',
    'If-Modified-Since': '0',
    'sec-ch-ua-mobile': '?0',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Dest': 'empty',
    'Referer': 'https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc',
    'Accept-Language': 'zh-CN,zh;q=0.9',
}

params = (
    ('leftTicketDTO.train_date', '2021-05-01'),
    ('leftTicketDTO.from_station', 'SHH'),
    ('leftTicketDTO.to_station', 'BJP'),
    ('purpose_codes', 'ADULT'),
)

response = requests.get('https://kyfw.12306.cn/otn/leftTicket/query', headers=headers, params=params,cookies=cookies)

print(response.json())

炫意HTML5 » 关于如何获得12306的列车信息

Java基础教程Android基础教程