Python 12306抢票脚本

请注意,编写或使用抢票脚本可能违反相关网站的服务条款和法律法规。以下内容仅供学习和了解技术原理之用,不鼓励或支持任何违反规定的行为。

Python 12306抢票脚本通常涉及以下几个步骤:

  1. 登录 :使用Python的requests库模拟登录12306网站,获取登录后的Cookie。

  2. 查询余票:定期发送请求到12306的余票查询接口,检查所需车次的余票情况。

  3. 提交订单:一旦检测到有余票,立即提交订单请求。

  4. 支付订单:在订单提交成功后,进行支付操作。

以下是一个非常简化的Python脚本示例,用于说明抢票脚本的基本逻辑:

复制代码
import requests
import time

# 12306登录后的Cookie
cookie = '你的12306登录Cookie'

# 余票查询URL
query_url = 'https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date=2023-04-30&leftTicketDTO.from_station=SHH&leftTicketDTO.to_station=BJP&purpose_codes=ADULT'

# 提交订单URL
order_url = 'https://kyfw.12306.cn/otn/confirmPassenger/initDc'

# 列车信息
train_info = {
    'train_no': '列车号',
    'from_station_telecode': '始发站电报码',
    'to_station_telecode': '终点站电报码',
    'start_train_time': '开车时间',
    'arrive_train_time': '到达时间',
    'second_date': '次日标识'
}

# 乘客信息
passenger_info = {
    'passengerTicketStr': 'T123456789012',
    'passengerName': '乘客姓名',
    'passengerType': '乘客类型',
    'mobileNo': '手机号',
    'passengerIdType': '证件类型',
    'passengerIdNo': '证件号码',
    'insure': '是否购买保险',
    'preferential': '优惠票类型'
}

# 发送余票查询请求
def query_tickets():
    headers = {'Cookie': cookie}
    response = requests.get(query_url, headers=headers)
    data = response.json()
    return data

# 提交订单
def submit_order():
    headers = {'Cookie': cookie}
    response = requests.post(order_url, json=passenger_info, headers=headers)
    return response.json()

# 主函数
def main():
    while True:
        data = query_tickets()
        if 'data' in data and data['data']['result'] == 'true':
            print("有余票,开始提交订单...")
            order_data = submit_order()
            if 'result' in order_data and order_data['result'] == 'true':
                print("订单提交成功,请尽快支付。")
                break
        time.sleep(5)  # 每隔5秒查询一次

if __name__ == '__main__':
    main()

请注意,这个脚本仅用于演示目的,实际的12306抢票脚本会更加复杂,并且需要处理验证码、登录验证、异常处理等问题。此外,12306网站可能有反爬虫机制,自动化脚本可能会被检测并封禁账号。在使用任何自动化工具之前,请确保你了解并遵守相关的法律法规和服务条款。

相关推荐
小九九的爸爸4 小时前
前端想要入门Agent开发,要具备哪些Python基础?
python·agent·ai编程
阿耶同学5 小时前
手把手教你用 LangGraph 搭建三层嵌套 Agent 架构
python·程序员
花酒锄作田1 天前
Pydantic校验配置文件
python
hboot1 天前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi1 天前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi1 天前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽1 天前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
用户8358086187911 天前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python
Warson_L2 天前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅2 天前
海天线算法的前世今生
python·计算机视觉