Python实例 一 12306抢票(二) 下单

本文详细介绍了如何使用Python进行12306火车票的刷票与下单操作,包括登录、查询车次信息、提交订单、获取购票人信息、检查订单、获取余票和排队人数、选座提交、获取订单ID以及确认回执信息等步骤。通过处理cookie来保持登录状态,并提供源码链接供读者参考。
摘要由CSDN通过智能技术生成

第二篇 刷票与下单

原文地址:http://www.tnblog.net/cz/article/details/241

解决"url":"/leftTicket/init","status":false,"httpstatus":200,"messages":["系统忙,请稍后重试"] 问题

1.记住登陆

    上一篇写了登陆:http://www.tnblog.net/cz/article/details/162

    为了方便调试 不让每次登陆都去扫码一遍,做一个缓存登陆的。

    登陆就是会话保持,一般依赖于cookie,token之类的信息保存到cookie中,请求的时候带着cookie去服务器,服务器就知道是同一个人。而请求使用的request库的session会自动保存每次服务器返回的cookie信息,下次请求带上该cookie ,所以能在运行时一直保持你登陆过的状态,但是重新运行程序就不行了,所以我们可以把上次的cookie信息存到文件中,下次运行的时候把cookie读取放到session中去 就哦了!

登录前session的cookie信息

登陆成功的

 

 

把cookie信息存入文件,与读取出来:

# 保存cookie
def saveCookie():
    _cookies = session.cookies.get_dict()
    # 取到session的cookie信息 取出来是键值对把他转化成字符串类型保存下来
    cookieStr = json.dumps(_cookies)
    with open('./cookies.txt', 'w') as f:
    f.write(cookieStr)
    print('记录cookie成功')

# 取出cookie
def getCookie():
    try:
        with open('./cookies.txt', 'r') as f:
        _cookie = json.load(f)
        # session的cookie是一个RequestsCookieJar类型的,把键值对转换为给他
        session.cookies = requests.utils.cookiejar_from_dict(_cookie)
    except FileNotFoundError:
        print('还未登陆过..')

 

保存成功之后 这样:

 

 

测试一下:

先登录

停止程序之后,直接读取cookie测试成功!!

 

 

 

好了开始进入刷票下单:

流程:

https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date=2019-02-14&leftTicketDTO.from_station=CQW&leftTicketDTO.to_station=TVW&purpose_codes=ADULT # 查询
https://kyfw.12306.cn/otn/leftTicket/submitOrderRequest # 提交下单请求
https://kyfw.12306.cn/otn/confirmPassenger/initDc # 获取一堆参数 后面的请求需要用到
https://kyfw.12306.cn/otn/confirmPassenger/getPassengerDTOs # 获取购票人信息 (乘车人信息)
https://kyfw.12306.cn/otn/confirmPassenger/checkOrderInfo # 检查订单信息 (是否能购票,是否有未支付的订单)
https://kyfw.12306.cn/otn/confirmPassenger/getQueueCount # 获取余票,与排队人数
https://kyfw.12306.cn/otn/confirmPassenger/confirmSingleForQueue #选座,确定提交
https://kyfw.12306.cn/otn/confirmPassenger/queryOrderWaitTime?random=1547780170175&tourFlag=dc&_json_att=&REPEAT_SUBMIT_TOKEN=a8dc2ed1a814b3067cfc32550c17e0ed # 排队
https://kyfw.12306.cn/otn/confirmPassenger/resultOrderForDcQueue # 确定下单成功回执

 

一、查询

地址:https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date=2019-02-14&leftTicketDTO.from_station=CQW&leftTicketDTO.to_station=TVW&purpose_codes=ADULT 
参数:{
      leftTicketDTO.train_date:2019-02-14  #购票日期
      leftTicketDTO.from_station:CQW #起点站
      leftTicketDTO.to_station:TVW #终点站
      purpose_codes:ADULT #票类型,ADULT普通票 
    }
返回:{"data":{"flag":"1","map":{"CUW":"重庆北","CXW":"重庆西","TVW":"潼南"},"result":["|列车运行图调整,暂停发售|77000D51030J|D5103|CXW|ICW|CXW|TVW|24:00|24:00|99:59|IS_TIME_NOT_BUY||20190214||W3|01|03|0|1|||||||||||||||||0|0|null","|列车运行图调整,暂停发售|77000D51170A|D5117|CUW|NIW|CUW|TVW|24:00|24:00|99:59|IS_TIME_NOT_BUY||20190214||W2|01|03|0|1|||||||||||||||||0|0|null","IQEEzf7ymvboXyZSxb%2Bvno%2FvYNEZbmuqDINQp%2Bmnh3k4f0fECt06bHNQPKk4Hlpp8k%2FEY%2FHY1hMb%0Aua7x0jZi61wrCfaoT%2FPBfZMLd4iBVRZurosYHAz0E191H6A5Imnmwng39zx10jq6Bj5eBTTWbH4U%0Ag5a1RfO85IpYBCUAfwSajgMxulbU63F6m2rH%2Bn3QV2djAIrKzIDYwjfdFAuLAfzMa5Sb0cV3nEG7%0AnW43VHcCPF9qB%2BvHUSuHaQcbaHdYztBgPpKcWEXeSXZ0Hekt8%2BIORs8NXs%2FMiGi%2FLkbi5NdZYb%2BJ%0Ax36wYA%3D%3D|预订|710000K1420H|K143|NNZ|CDW|CXW|TVW|09:18|10:36|01:18|Y|t47JENLelAwrUH8jO7aObclgoEFHNBYIMu9USdXqyieI0PymUpkzmENrhno%3D|20190213|3|Z1|11|12|0|0||||2|||有||有|16|||||10401030|1413|0|0|null","MUyH%2FHTEfnPsZwsT%2B2JzqnO21xQ5sGp0nnfSH52Dxg82ZvrybT7nChBhBxb3TsvMjtJyUJx89uNQ%0AMCrH9ekzbik0zw54JI4FG7uavZBLFJwJFKM6jXIhI6akPir9cafU%2BO07Fq2oHvjihljCWeydEtMq%0AHltj9Cl6HqKGR9xxZS1UsSAWqyjFBxPFLPFkPjn1LwFH%2FnvW1li%2FMEDeRugFkjsdIzBNPnrhDB3I%0A%2Bi022Xdvfv8XW3YA0vrhyYKwZU%2FGAR52EgfaJAMKGomYC%2F6HFjCas%2BCl8big1Vkw%2FuIAObayl7Is%0AtwecSA%3D%3D|预订|710000K8720H|K873|ZJZ|ICW|CXW|TVW|09:24|10:52|01:28|Y|MtttfIDxq6HqNo9XM%2BstI4zCQgaSjr%2Fce9ZED1rem4EoS1%2FuzFDmxLqVGMs%3D|20190213|3|Z1|18|19|0|0||||无|||有||有|有|||||10401030|1413|0|0|null","|列车运行图调整,暂停发售|77000D514570|D5145|CUW|TVW|CUW|TVW|24:00|24:00|99:59|IS_TIME_NOT_BUY||20190214||W2|01|03|0|1|||||||||||||||||1|0|null","|预订|4e0000D63208|D633|WHN|CDW|CUW|TVW|13:59|14:51|00:52|N|1y9bvC04rZ6kAvLar8uM3MsUNOn3MncbI4gVoC5mFGj1Ltnz|20190214|3|N3|13|15|0|0|||||||无||||无|无|||O0M0O0|OMO|0|0|null","|列车运行图调整,暂停发售|77000D513908|D5139|CUW|NUW|CUW|TVW|24:00|24:00|99:59|IS_TIME_NOT_BUY||20190214||W2|01|03|0|1|||||||||||||||||0|0|null","|列车停运|71000D178202|D1783|NFZ|ICW|CXW|TVW|24:00|24:00|99:59|IS_TIME_NOT_BUY||20190214||Z1|17|18|0|1|||||||||||||||||0|0|null","|预订|6c000D180604|D1807|IZQ|ICW|CXW|TVW|16:01|16:48|00:47|N|0IsJRXfUvj4fKLm47STmvIB%2BzwWrdhrzCQqwSJd8lsG62utJ|20190214|3|Q9|13|14|0|0|||||||无||||无|||无|O0F0O0|OFO|0|0|null","43XpgRuIos5DmDfOdyu0HdaV7DDZSadtZ7uNBtK%2FIySnx0giFWZSvBddQ%2FPVAoKo%2FINIK7TD5jyE%0AGZv3ODius1%2BTL3IK6NIBVTdxysFwgmI3cKUME4TrOB2Pf1mw%2BFjSirjYyuvekN315V%2BbNGdVn62L%0Ai9%2FJxuXCZs8g0Akf1jhgE7VN6VNnxl8DGxwwz55RJ%2FGes5qp%2FvD2qa%2F%2FDf81Q4tSkXKCBlF5d2Oh%0ANjG49IAciQHLaX7gXTzzxY7HxJbmS7TBMNfxQCdtFa5%2Fm8NEAZem%2FLZqLU4%2BpDyZWyqoUO4%3D|预订|5l000D237340|D2373|NKH|ICW|CUW|TVW|16:56|17:47|00:51|Y|pp0%2Ft2%2BQaSwUfIh63Y%2FXtf%2FVMBxdG0SLx%2FK1MiAVidpk0ak%2B|20190214|3|H2|15|17|0|0|||||||无||||2|1|||O0M0O0|OMO|0|0|null","NwCfMpWhQYGrv9zKy%2FE2bJHhqOoVHXTgo5hTboBBIe%2FKzro2zxrnAiA4x1lswwh5upe5V31vZstu%0AK0eMiMwDCjgTeU4CwQtsJdOE%2FFN3yY6AN4D%2FS%2Fh1r1pC3QWwUfbXueeXWVrsXQuv7gxyz8wQjXDH%0Ah0%2Bfm2VUKochP2Te910gF9I5oWcUk4Gkih44Ey7u2hQmjW8QN%2BtYdmvCn5LZ7QW3GOZHWSzePrZJ%0ALr3Kyh2JqCZokd9MguNCNb99pGH5AV%2B97%2F3ek7cN1F872WL0HTRZsnS3wkhHwLowo4nWqmE%3D|预订|77000D514708|D5147|CUW|TVW|CUW|TVW|17:13|17:57|00:44|Y|CLy0iYHVYp2Mctmf3%2FRbBmuCCUGyeyU8JF%2Bjolnv%2FfplIrD2|20190214|3|W2|01|03|0|0|||||||有||||有|19|||O0M0O0|OMO|1|0|null","%2FC
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值