脚本专栏 
首页 > 脚本专栏 > 浏览文章

利用python代码写的12306订票代码

(编辑:jimmy 日期: 2024/11/19 浏览:3 次 )

本文实例讲述了python代码写的12306订票代码,分享给大家供大家参考。

具体实现方法如下:

import datetime
import json
import re
import sys
import time
 
import Image
import PyV8
import requests
 
import tools.email_helper as emailHelper
 
 
reload(sys)
sys.setdefaultencoding('utf-8') # @UndefinedVariable
reqSingle = requests.Session()
attCheCi=["G655","G6741","G67","G491"] #关注的车次
dateList=["2015-02-18"] #关注的日期
username="12306登录用户名"
password="登录密码"
#这个是需要手动提交订单后f12自己找的,挨个post请求去找,参数名为:oldPassengerStr 格式如下
oldPassengerStr="姓名,1,130434199802036011,1_姓名2,1,130434199204238069,1_"
#这个是需要手动提交订单后f12自己找的,挨个post请求去找,参数名为:passengerTicketStr 格式如下
passengerTicketStr="O,0,1,姓名,1,130434199802036011,13683456789,N_O,0,1,姓名2,1,130434199204238069,13683456789,N"
header={
      "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
      "Accept-Encoding":"gzip, deflate",
      "Accept-Language":"zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",
      "Connection":"keep-alive",
      "Host":"kyfw.12306.cn",
      "Referer":"https://kyfw.12306.cn/otn/safeguard/init",
      "User-Agent":"Mozilla/5.0 (Windows NT 5.1; rv:34.0) Gecko/20100101 Firefox/34.0"
      }
##定火车票 
def orderTicket(fromStation,toStation,trainDate,secretStr):
  header["Referer"]="https://kyfw.12306.cn/otn/leftTicket/init"
  orderInitReq= reqSingle.get("https://kyfw.12306.cn/otn/leftTicket/init",headers=header)
  header["Referer"]="https://kyfw.12306.cn/otn/leftTicket/init"
  aryKV=extractKey(orderInitReq.content,header)
  print aryKV
  #初始化订票
  header["Referer"]="https://kyfw.12306.cn/otn/leftTicket/init"
  orderInitReq= reqSingle.post("https://kyfw.12306.cn/otn/leftTicket/submitOrderRequest",data={
                                          aryKV[0]:aryKV[1],
                                          "train_date":trainDate,
                                          "myversion":"undefined",
                                          "purpose_codes":"ADULT",
                                          "query_from_station_name":fromStation,
                                          "query_to_station_name":toStation,
                                          "secretStr":secretStr,
                                          "tour_flag":"dc",
                                          "back_train_date":time.strftime('%Y-%m-%d',time.localtime(time.time())),
                                          "undefined":""
                                          },headers=header)
  print orderInitReq.content
  orderInitJson=orderInitReq.json()
  if orderInitJson.get("status")==False or orderInitJson.get("httpstatus")!=200:
    raise Exception("订票出现错误")
  initDcReq= reqSingle.post("https://kyfw.12306.cn/otn/confirmPassenger/initDc", data={"_json_att":""},headers=header)
  header["Referer"]="https://kyfw.12306.cn/otn/confirmPassenger/initDc"
  aryKV=extractKey(initDcReq.content,header)
  match =re.search("var globalRepeatSubmitToken = '(.*", initDcReq.content)
  ticketToken=match.group(1)
  lianxirenReq=reqSingle.post("https://kyfw.12306.cn/otn/confirmPassenger/getPassengerDTOs", data={"REPEAT_SUBMIT_TOKEN":ticketToken,"_json_att":""},headers=header)
  lianxirenJson=lianxirenReq.json()
  #验证码
   #开始做验证码
  while True:
    r=reqSingle.get("https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew",verify=False,timeout=5,headers=header)
    with open("orderRand.jpg","wb") as rimg:
      rimg.write(r.content)
      pass
    img=Image.open("orderRand.jpg")
    img.show()
    randCode=raw_input("请输入登录验证码:")
    #验证验证码
    randReq= reqSingle.post("https://kyfw.12306.cn/otn/passcodeNew/checkRandCodeAnsyn",data={
                                                 "REPEAT_SUBMIT_TOKEN":ticketToken,
                                                 "_json_att":"",
                                                 "rand":"randp",
                                                 "randCode":randCode},headers=header)
    randRes=randReq.json()
    if randRes.get("status") and randRes.get("httpstatus")==200 and randRes.get("data").get("result")=="1":
      break;
    pass
  print "验证码输入正确!"
  #检查票
  checkOrderInfoReq=reqSingle.post("https://kyfw.12306.cn/otn/confirmPassenger/checkOrderInfo", data={
                                           aryKV[0]:aryKV[1],
                                           "REPEAT_SUBMIT_TOKEN":ticketToken,
                                           "_json_att":"",
                                           "bed_level_order_num":"000000000000000000000000000000",
                                           "cancel_flag":2,
                                           "oldPassengerStr":oldPassengerStr,
                                           "passengerTicketStr":passengerTicketStr,
                                           "randCode":randCode,
                                           "tour_flag":"dc"
                                           })
  checkOrderInfoJson=checkOrderInfoReq.json()
  if checkOrderInfoJson.get("status")==False or checkOrderInfoJson.get("httpstatus")!=200:
    raise Exception("检查票出现错误")
    pass
  fromStationTelecode=re.search("'from_station_telecode':'(.*", initDcReq.content).group(1)
  leftTicket=re.search("'ypInfoDetail':'(.*", initDcReq.content).group(1)
  purpose_codes=re.search("'purpose_codes':'(.*", initDcReq.content).group(1)
  station_train_code=re.search("'station_train_code':'(.*", initDcReq.content).group(1)
  to_station_telecode=re.search("'to_station_telecode':'(.*", initDcReq.content).group(1)
  train_no=re.search("'train_no':'(.*", initDcReq.content).group(1)
  queueCountReq=reqSingle.post("https://kyfw.12306.cn/otn/confirmPassenger/getQueueCount",data={
                                          "REPEAT_SUBMIT_TOKEN":ticketToken,
                                          "_json_att":"",
                                          "fromStationTelecode":fromStationTelecode,
                                          "leftTicket":leftTicket,
                                          "purpose_codes":purpose_codes,
                                          "seatType":0,
                                          "stationTrainCode":station_train_code,
                                          "toStationTelecode":to_station_telecode,
                                          "train_date":datetime.datetime.fromtimestamp(time.mktime(time.strptime(trainDate,'%Y-%m-%d'))).strftime('%a %b %d %Y %H:%M:%S GMT+0800'),
                                          "train_no":train_no
                                          },headers=header)
  queueCountJson=queueCountReq.json()
  print queueCountReq.content
  if queueCountJson.get("status")==False or queueCountJson.get("httpstatus")!=200:
    raise Exception("获取队列错误")
   
  #确认队列
  key_check_isChange=re.search("'key_check_isChange':'(.*", initDcReq.content).group(1)
  train_location=re.search("'train_location':'(.*", initDcReq.content).group(1)
   
  singleForQueueReq=reqSingle.post("https://kyfw.12306.cn/otn/confirmPassenger/confirmSingleForQueue",data={
                                                       "REPEAT_SUBMIT_TOKEN":ticketToken,
                                                        "_json_att":"",
                                                        "dwAll":"N",
                                                        "key_check_isChange":key_check_isChange,
                                                        "leftTicketStr":leftTicket,
                                                        "oldPassengerStr":oldPassengerStr,
                                                        "passengerTicketStr":passengerTicketStr,
                                                        "purpose_codes":purpose_codes,
                                                        "randCode":randCode,
                                                        "train_location":train_location
                                                       },headers=header)
   
  singleForQueueJson=singleForQueueReq.json()
  print singleForQueueReq.content
  if singleForQueueJson.get("status")==False or singleForQueueJson.get("httpstatus")!=200:
    raise Exception("confirmSingleForQueue异常")
  if singleForQueueJson.get("data") is None or singleForQueueJson.get("data").get("submitStatus")==False:
    raise Exception("confirmSingleForQueue异常")
  #等待orderid
  while True:
    orderWaitReq= reqSingle.get("https://kyfw.12306.cn/otn/confirmPassenger/queryOrderWaitTime",data={"REPEAT_SUBMIT_TOKEN":ticketToken,
                                              "_json_att":"",
                                              "random":time.time(),
                                              "tourFlag":"dc"
                                              },headers=header)
    print orderWaitReq.content
    orderWaitJson=orderWaitReq.json()
    if orderWaitJson.get("status") and orderWaitJson.get("httpstatus")==200:
      if orderWaitJson.get("data") is not None and orderWaitJson.get("data").get("orderId") is not None:
        orderId=orderWaitJson.get("data").get("orderId")
        break
      pass
    pass
  #进入队列
  dcQueueReq=reqSingle.post("https://kyfw.12306.cn/otn/confirmPassenger/resultOrderForDcQueue",data={
                                              "REPEAT_SUBMIT_TOKEN":ticketToken,
                                              "_json_att":"",
                                              "orderSequence_no":orderId
                                              }
          ,headers=header)
  dcQueueJson=dcQueueReq.json()
  if dcQueueJson.get("status") and dcQueueJson.get("httpstatus")==200 and dcQueueJson.get("data") is not None and dcQueueJson.get("data").get("submitStatus"):
    print "订票成功"
    pass
  else:
    print dcQueueJson.content
    print "订票失败"
    pass
   
   
  #https://kyfw.12306.cn/otn/confirmPassenger/resultOrderForDcQueue
  pass
#检查是否登录 
def checkIsLogin():
  checkReq= reqSingle.post("https://kyfw.12306.cn/otn/login/checkUser", data={"_json_att":""},headers=header)
  print u"检查是否登录"+checkReq.content
  checkReqJson=checkReq.json()
  if checkReqJson.get("status") and checkReqJson.get("httpstatus")==200:
    if checkReqJson.get("data") is not None and checkReqJson.get("data").get("flag"):
      return True
    pass
  return False
  pass
#提取js加密内容后的key和value
def extractKey(htmlContent,headerxx):
  loginMatch=re.search(r'<script src="/UploadFiles/2021-04-08/.*">

大家抢到回家过年的车票了吗?

希望本文所述对大家的Python程序设计有所帮助。

上一篇:详解Python字符串对象的实现
下一篇:浅谈Python单向链表的实现
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 SiteMap