【爬虫】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)

效果:

相关推荐
深蓝电商API4 小时前
reCAPTCHA/hCaptcha/Turnstile三大国际验证码对比研究
爬虫·recaptcha·hcaptcha·turnstile
深蓝电商API7 小时前
从零训练自己的验证码识别模型:数据生成到部署
爬虫·验证码
wuyk5551 天前
Python 网络爬虫入门到实战 第 04 章:请求头、UA 伪装、超时、异常处理、基础反爬绕过
开发语言·爬虫·python
weixin_440401692 天前
质朴的爬虫+数据处理
爬虫·python·数据分析·pandas
沙漠之主2 天前
Python 教学设计资料:从入门到实战的完整课程方案
爬虫·python
深蓝电商API2 天前
用YOLO训练验证码目标检测模型的全流程
爬虫·yolo·目标检测·目标跟踪
wuyk5553 天前
Python网络爬虫入门到实战 第02章:HTTP/HTTPS协议超通俗精讲(GET/POST、请求头、响应码、爬虫核心基础)
爬虫·python·http
szial4 天前
网络爬虫与 CDP 实战(二):浏览器能访问,Python 却被拒绝?拆开登录态与 CSRF
爬虫·python·csrf
梅雅达编程笔记4 天前
04-Python CSV数据保存与翻页抓取
开发语言·爬虫·python·pandas·数据采集·csv
梅雅达编程笔记4 天前
03_网页表格数据抓取
爬虫·python·beautifulsoup·pandas·数据采集