【爬虫】12306查票

城市代码:

没有加密,关键部分:

完整代码:

复制代码
import json
import requests
with open(r'E:\学习文件夹(关于爬虫)\项目实战\12306\城市代码.json','r',encoding='utf-8') as f:
    city_code=f.read()
city = json.loads(city_code)
go_city=input('请输入出发地:')
to_city=input('请输入目的地:')
go_time = input('请输入出发时间(格式为:2025-05-12):')
cookies = {
    '_uab_collina': '174701576232025273188693',
    'JSESSIONID': '53478FDCC4B9E8626029BE6684F0F1CD',
    'BIGipServerotn': '1306067210.50210.0000',
    'BIGipServerpassport': '803733770.50215.0000',
    'guidesStatus': 'off',
    'highContrastMode': 'defaltMode',
    'cursorStatus': 'off',
    'route': 'c5c62a339e7744272a54643b3be5bf64',
    '_jc_save_fromStation': '%u5E7F%u5DDE%u5317%2CGBQ',
    '_jc_save_toStation': '%u5E7F%u5DDE%u5357%2CIZQ',
    '_jc_save_fromDate': '2025-05-12',
    '_jc_save_toDate': '2025-05-12',
    '_jc_save_wfdc_flag': 'dc',
}

headers = {
    'Accept': '*/*',
    'Accept-Language': 'zh,zh-CN;q=0.9,zh-TW;q=0.8',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    # 'Cookie': '_uab_collina=174701576232025273188693; JSESSIONID=53478FDCC4B9E8626029BE6684F0F1CD; BIGipServerotn=1306067210.50210.0000; BIGipServerpassport=803733770.50215.0000; guidesStatus=off; highContrastMode=defaltMode; cursorStatus=off; route=c5c62a339e7744272a54643b3be5bf64; _jc_save_fromStation=%u5E7F%u5DDE%u5317%2CGBQ; _jc_save_toStation=%u5E7F%u5DDE%u5357%2CIZQ; _jc_save_fromDate=2025-05-12; _jc_save_toDate=2025-05-12; _jc_save_wfdc_flag=dc',
    'If-Modified-Since': '0',
    'Pragma': 'no-cache',
    'Referer': 'https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc&fs=%E5%B9%BF%E5%B7%9E%E5%8C%97,GBQ&ts=%E5%B9%BF%E5%B7%9E%E5%8D%97,IZQ&date=2025-05-12&flag=N,N,Y',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 115Browser/27.0.6.3',
    'X-Requested-With': 'XMLHttpRequest',
    'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="125", "Google Chrome";v="125"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"macOS"',
}

params = {
    'leftTicketDTO.train_date': f'{go_time}',
    'leftTicketDTO.from_station': f'{city[go_city]}',
    'leftTicketDTO.to_station': f'{city[to_city]}',
    'purpose_codes': 'ADULT',
}

response = requests.get('https://kyfw.12306.cn/otn/leftTicket/queryG', params=params, cookies=cookies, headers=headers)
# print(response.status_code)
data = response.json()['data']['result']
for i in data:
    index = i.split('|')
    # page=0
    # for j in index:
    #     print(j,page,sep='===序号为')
    #     page+=1
    checi=index[3]
    go_time=index[8]
    to_time=index[9]
    time=index[10]
    vip=index[32]
    ydz=index[31]
    edz=index[30]
    dict={
        '车次': checi,
        '出发时间': go_time,
        '到达时间': to_time,
        '历时': time,
        '商务座': vip,
        '一等座': ydz,
        '二等座': edz
    }
    print(dict)

效果:

相关推荐
广州山泉婚姻2 小时前
如何设计一个高效的网页爬虫?
爬虫
程序小武4 小时前
爬虫实战指南:从定位数据到解析请求的全流程解析
爬虫
星期天要睡觉9 小时前
python网络爬虫(第三章/共三章:驱动浏览器窗口界面,网页元素定位,模拟用户交互(输入操作、点击操作、文件上传),浏览器窗口切换,循环爬取存储)
爬虫·python·交互
AI 嗯啦12 小时前
爬虫实战案例(两个)
爬虫
过往入尘土1 天前
网络爬虫的详细知识点
爬虫
狗都不学爬虫_1 天前
JS逆向 - 东、深、昆航查询参数
爬虫·网络爬虫·wasm
失败又激情的man1 天前
爬虫逆向之JS混淆案例(全国招标公告公示搜索引擎 type__1017逆向)
爬虫
赴3351 天前
python网络爬虫之selenium库(二)
爬虫·python·selenium
爬点儿啥1 天前
[JS逆向] 微信小程序逆向工程实战
开发语言·javascript·爬虫·微信小程序·逆向
AI 嗯啦2 天前
爬虫小知识(二)网页进行交互
运维·服务器·开发语言·爬虫·python·交互