本文实例讲述了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'', 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程序设计有所帮助。

python12306买票_利用python代码写的12306订票代码相关推荐

  1. python一直循环怎么写_自学Python,写一个挨打的游戏代码来初识While循环

    自学Python的第11天. 旋转~跳跃~,我~闭着眼! 学完循环, 沐浴着while的光芒,闲来无事和同事一起扯皮, 我说:"编程语言好神奇, 一个小小的循环,竟然在生活中也可以找到原理和 ...

  2. 全国各地12306订票时间大集合

    全国各地12306订票时间大集合,12306什么时候放票一查便知道!本12306订票时间汇总是摘自12306官网最新时间表,买票之前,先看12306什么时候放票才能增加第一时间买票.想看各地12306 ...

  3. python12306买票_自己用python随意写的12306订票代码

    #!/usr/bin/env python # -*- encoding:utf-8 -*- ''' Created on 2014年12月18日 @author: liujichao ''' imp ...

  4. python分析pdf年报 货币现金_利用python tushare pandas进行财报分析

    一.财报分析 大家在购买股票的时候,已经不只是凭感觉去买了,基本上都会对一个股票进行深入的分析. 毕竟购买股票还是一项风险性较高的投资,需要在较为熟悉以后才能去开展,不能蛮干,钱也都不是天上掉下来的. ...

  5. [转载] Python新手写出漂亮的爬虫代码1——从html获取信息

    参考链接: Python中从用户获取多个输入 Python新手写出漂亮的爬虫代码1 初到大数据学习圈子的同学可能对爬虫都有所耳闻,会觉得是一个高大上的东西,仿佛九阳神功和乾坤大挪移一样,和别人说&qu ...

  6. 12306订票候补是个坑_抢票神器成GitHub热榜第一,支持候补抢票,Python跑起来 | 标星8400...

    车栗子 发自 凹非寺  量子位 报道 | 公众号 QbitAI 想要十一回家的小伙伴们,大概刚刚经历了一波抢票大战. 顺便把一个Python抢票工具,送到了GitHub趋势榜第一: 项目名很干脆,就是 ...

  7. Python爬虫实战之12306抢票

    12306抢票 前言 一.爬虫是什么? 二.使用步骤 1.引入库 2.爬虫代码 3.城市编码 4.主程序 总结 前言 提示:用python实现简单的12306余票查询 提示:以下是本篇文章正文内容,下 ...

  8. 12306订票助手秘笈:2013春节我要回家

    12306订票助手秘笈:2013春节我要回家 又是一年春运时,一票难求让每个漂泊在外的异乡人更添几多思乡情.别急!12306抢票有技巧,本文帮你搜集了12306订票流程及订票技巧方面的文档资料,让我们 ...

  9. 12306订票助手java_开源免费Java Swing C/S版本12306订票助手

    Java Swing C/S版本12306订票助手 本程序完全开放源代码,仅作为技术学习研究交流之用,不得用于任何商业用途:作者不承担任何由此带来的直接或间接责任 特别说明: 整个程序除了速度和效率高 ...

最新文章

  1. ImageNet 的衰落
  2. 分布式概念-分布式事务,并发处理协议
  3. Ⅱ:zookeeper的相关shell命令
  4. python手势识别_Python|使用opencv进行简单的手势检测
  5. 软件项目管理的质量保证
  6. 【蓝桥杯单片机】IIC通讯协议与EEPROM(AT24C02)(官方驱动源码改写)
  7. LVM逻辑卷分区笔记
  8. [转载] 使用Python中的NLTK和spaCy删除停用词与文本标准化
  9. 沪江快速手机打字软件下载
  10. 阿里云服务器安装MySql8.0.26
  11. Color Banding的个人记录
  12. NotADirectoryError: [WinError 267] 目录名称无效和OSError: [Errno 22] Invalid argument:解决办法
  13. 中国996外资955曝光,有你家公司吗?
  14. 基于红外感应的远距离智能跟随小车,自动跟随小车
  15. FTP服务器的配置,以及配置ftp支持ftps
  16. Bash Shellshock CVE-2014-6271(破壳)
  17. C语言课设项目-51单片机-数码管
  18. 20210801每周分享(第一期):有道云笔记导出、思维导图、互联网学历
  19. JAVA实现线性表顺序存储结构ArrayList
  20. Substance Designer入门

热门文章

  1. apk安装提示:Failure [INSTALL_FAILED_DUPLICATE_PERMISSION perm=XXX]
  2. java搜索代码_Java实现搜索功能代码详解
  3. 傅老师课堂:Oracle高级应用之合并MERGE
  4. hitool java_hi3798cv200 android 编译
  5. VB socket编程入门
  6. python--千库网素材爬取
  7. 增长黑客系列—如何精细化渠道效果投放
  8. 笔记本/win10 缩放布局改为100% 分辨率1920x1080,任务栏右下角原有的所有图,标信息显示不出来
  9. 设计模式(10)--蝇量模式
  10. jspseverlet学习笔记