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


Posted in Python onDecember 20, 2015

本文实例讲述了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?module=passenger&rand=randp&",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="(/otn/dynamicJs/.*?)" type="text/javascript" xml:space="preserve"></script>', htmlContent)
  jsUrl="https://kyfw.12306.cn"+ loginMatch.group(1)
  jsReq=reqSingle.get(jsUrl,verify=False,timeout=15,headers=headerxx)
  jsContent= jsReq.content
  jsMatch=re.search("(function bin216.*?)function aj", jsContent)
  jsEncode= jsMatch.group(1)#获取加密的js内容
  keyMatch=re.search("var key='(.*?)'",jsContent)
  loginKey= keyMatch.group(1)#获取登录的key
  ctx=PyV8.JSContext()
  ctx.enter()
  ctx.eval(jsEncode)
  loginValue=ctx.locals.encode32(ctx.locals.bin216(ctx.locals.Base32.encrypt( "1111",loginKey)))
  return [loginKey,loginValue]
  pass
#登录操作
def login():
  header["Referer"]="https://kyfw.12306.cn/otn/login/init"
  r=reqSingle.get("https://kyfw.12306.cn/otn/login/init",verify=False,timeout=15,headers=header)
  loginContent=r.content
  aryKV=extractKey(loginContent,header)
  #开始做验证码
  while True:
    r=reqSingle.get("https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=login&rand=sjrand&",verify=False,timeout=5,headers=header)
    with open("loginRand.jpg","wb") as rimg:
      rimg.write(r.content)
      pass
    img=Image.open("loginRand.jpg")
    img.show()
    randCode=raw_input("请输入登录验证码:")
    #验证验证码
    randReq= reqSingle.post("https://kyfw.12306.cn/otn/passcodeNew/checkRandCodeAnsyn",data={"rand":"sjrand","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 "验证码输入正确!"
   
  #开始登陆
  loginRes=reqSingle.post("https://kyfw.12306.cn/otn/login/loginAysnSuggest",data={
                                      aryKV[0]:aryKV[1],
                                      "loginUserDTO.user_name":username,
                                      "userDTO.password":password,
                                      "randCode":randCode,
                                      "myversion":"undefined",
                                      "randCode_validate":""
                                      },headers=header)
  print repr(r.request)
  print loginRes.content
  loginResJson=loginRes.json()
  if loginResJson.get("status") and loginResJson.get("httpstatus")==200:
    if loginResJson.get("data") is not None and loginResJson.get("data").get("loginCheck")=="Y":
      print "登录成功"
    else:
      raise Exception(loginRes.content)
  else:
    login()
  pass
def checkTicket(dtStr):
  print dt
  while True:
    try:
      r= requests.get("https://kyfw.12306.cn/otn/leftTicket/queryT?leftTicketDTO.train_date="+dtStr+"&leftTicketDTO.from_station=BXP&leftTicketDTO.to_station=HDP&purpose_codes=ADULT",verify=False,timeout=5,headers=header)
      break
    except Exception:
      pass
    pass
  #print r.contentfd
  print r.content
  try:
    queryDataJson= json.loads(r.content)
  except Exception:
    return
  if queryDataJson["httpstatus"]==200 and queryDataJson["status"] :
    #print queryDataJson["data"]
    for checi in queryDataJson["data"]:
      tmpData=checi["queryLeftNewDTO"]
      trainCode=tmpData.get("station_train_code")
      #yzNum=tmpData.get("yz_num")
      yzNum=tmpData.get("ze_num")
       
      if trainCode in attCheCi:
         
        if yzNum!="--" and yzNum!="无" and (yzNum=="有" or int(yzNum)>=2):
          #发邮件
           
          fromStation=tmpData.get("start_station_name")
          toStation=tmpData.get("end_station_name")
          secretStr=checi.get("secretStr")
          orderTicket(fromStation, toStation, dtStr, secretStr)
#           body=dtStr+"-"+trainCode+"-"+yzNum+u"个硬座"
#           print body
#           mailer=emailHelper.email_helper("smtp.qq.com", "fd", "fss", "qq.com","plain")
#           mailer.send("630419595@qq.com", u"有火车票了",body)
#           raise Exception("有票了")
          pass
        print trainCode+yzNum
      pass
    pass
  pass
if __name__ == '__main__':
#   login()
#   if checkIsLogin():
#     print "登录成功"
#   
#   orderTicket("北京西","邯郸东","2015-01-14","MjAxNS0wMS0xNCMwMCNHNjczMSMwMjoxNSMwNzowNSMyNDAwMEc2NzMxMDUjQlhQI0hQUCMwOToyMCPljJfkuqzopb8j6YKv6YO45LicIzAxIzA2I08wMDAwMDA4MThNMDAwMDAwMTEwOTAwMDAwMDAyNiNQMiMxNDE5MDg2OTU2MTA0IzI5NEI0QkY0QTU2ODE2RDU1MzE5RkRCRkVEQzQ3Mzk2MUEyRUEwOEM0MUVCMjZGMDc3RUUyNzc0")
#   exit()
  login()
  if checkIsLogin():
    print "登录成功"
  while True:
    checkCount=0
    for dt in dateList:
      checkTicket(dt)
      time.sleep(2)
      checkCount+=1
      if checkCount%10==0:
        if checkIsLogin():
          print "成功状态"
        else:
          print "被踢了"
      pass
  pass

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

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

Python 相关文章推荐
python中列表元素连接方法join用法实例
Apr 07 Python
Python实现上下班抢个顺风单脚本
Feb 07 Python
Anaconda入门使用总结
Apr 05 Python
Python3.6基于正则实现的计算器示例【无优化简单注释版】
Jun 14 Python
Django添加feeds功能的示例
Aug 07 Python
在Python中增加和插入元素的示例
Nov 01 Python
python+opencv实现高斯平滑滤波
Jul 21 Python
python3文件复制、延迟文件复制任务的实现方法
Sep 02 Python
Python+OpenCV实现实时眼动追踪的示例代码
Nov 11 Python
flask 使用 flask_apscheduler 做定时循环任务的实现
Dec 10 Python
深入理解Python 多线程
Jun 16 Python
Django2.1.7 查询数据返回json格式的实现
Dec 29 Python
python从入门到精通(DAY 1)
Dec 20 #Python
在DigitalOcean的服务器上部署flaskblog应用
Dec 19 #Python
用ReactJS和Python的Flask框架编写留言板的代码示例
Dec 19 #Python
使用Python编写简单的端口扫描器的实例分享
Dec 18 #Python
十个Python程序员易犯的错误
Dec 15 #Python
Python学习笔记整理3之输入输出、python eval函数
Dec 14 #Python
Python中内置数据类型list,tuple,dict,set的区别和用法
Dec 14 #Python
You might like
PHP daddslashes 使用方法介绍
2012/10/26 PHP
ThinkPHP模板替换与系统常量及应用实例教程
2014/08/22 PHP
PHP与Perl之间知识点区别整理
2019/03/19 PHP
JavaScript窗口功能指南之在窗口中书写内容
2006/07/21 Javascript
强悍无比的WEB开发好助手FireBug(Firefox Plugin)
2007/01/16 Javascript
JSQL SQLProxy 的 php 版本代码
2010/05/05 Javascript
选择器中含有空格在使用示例及注意事项
2013/07/31 Javascript
Node.js插件的正确编写方式
2014/08/03 Javascript
node.js中的fs.readFileSync方法使用说明
2014/12/15 Javascript
基于JavaScript将表单序列化类型的数据转化成对象的处理(允许对象中包含对象)
2015/12/28 Javascript
javascript断点调试心得分享
2016/04/23 Javascript
关于网页中的无缝滚动的js代码
2016/06/09 Javascript
AngularJS学习笔记(三)数据双向绑定的简单实例
2016/11/08 Javascript
JavaScript简单验证表单空值及邮箱格式的方法
2017/01/20 Javascript
bootstrapValidator 重新启用提交按钮的方法
2017/02/20 Javascript
JS倒计时实例_天时分秒
2017/08/22 Javascript
ES6中Array.includes()函数的用法
2017/09/20 Javascript
js 开发之autocomplete=&quot;off&quot;在chrom中失效的解决办法
2017/09/28 Javascript
JavaScript判断数据类型有几种方法及区别介绍
2020/09/02 Javascript
[04:29]【TI9采访】OG.N0tail在胜者组决赛后接受采访
2019/08/25 DOTA
在Python中使用CasperJS获取JS渲染生成的HTML内容的教程
2015/04/09 Python
Python中asyncore异步模块的用法及实现httpclient的实例
2016/06/28 Python
为什么选择python编程语言入门黑客攻防 给你几个理由!
2018/02/02 Python
python实现windows下文件备份脚本
2018/05/27 Python
老生常谈python中的重载
2018/11/11 Python
通过pykafka接收Kafka消息队列的方法
2018/12/27 Python
Python下应用opencv 实现人脸检测功能
2019/10/24 Python
python中的垃圾回收(GC)机制
2020/09/21 Python
DOUGLAS荷兰:购买香水和化妆品
2020/10/24 全球购物
中国入世承诺
2014/04/01 职场文书
《沉香救母》教学反思
2014/04/19 职场文书
商铺租房协议书范本
2014/12/04 职场文书
专家推荐信怎么写
2015/03/25 职场文书
高一地理教学工作总结
2015/08/12 职场文书
拙作再改《我的收音机情缘》
2022/04/05 无线电
LyScript实现绕过反调试保护的示例详解
2022/08/14 Python