大家好,欢迎来到IT知识分享网。
第二篇 刷票与下单
原文地址: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","%2FCq%2FwdowS1S9VIqsBpnF2KRBh9cwXXdvrkE7Ni7Q2Yo%2F5hpvDoHAw%2FwYScidhgJuXAOY7%2B6E1Nln%0AfusLNDgv3zWGzFWW73htly%2Fo05MnO1hNMer5dhSDFMx3M2e2m4DYiSJ6wCdleKPb0%2FQc%2B46QGJbo%0A458zqWY%2F1CVFCWhCrd3w3RGHDQ2MqqzcaYkxvxSdcdpd3B1Xoy%2BBZjTNH6gEccvbvW%2BTmHZcP7S2%0ADLZg9NaARQD1cxGCc3VMnEE5kOxTDDuAL66PfFy%2B1hzHMz0kjLURhHrGie7rFzC%2Fzo64HxM%3D|预订|5l0000D35273|D353|AOH|ICW|CUW|TVW|18:11|18:56|00:45|Y|PEDp1LSroEHChj4TmdO54rOKNNaLWQ%2F3PWTrFnOf0Gmhulg9|20190214|3|H2|18|19|0|0|||||||无||||2|2|||O0M0O0|OMO|0|0|null","eZJHEjCjbrawzJcm8DtAxZjjT7uZZe%2Bw23ZW5o3WiK6sWUjGu4bAnn1AqRTGb0xoH4tUKbrjMQAz%0AyXb74lN6G3kDNrYCi66g%2BNKfci2BXK6Hd2fMEFi0PoAcBtOCXEFbXfA7x%2BSYaFxNh4P2nLtkQ46i%0AMY%2BTkporMHoy2I8Dq7MFekl%2FuPbLUlfEJlZ69NxPaYETZ3TPfW8aHKstvQ72CGjriLvdkpeK7sYj%0A9rRG5RB%2BzatWKpJYTJwC1pfiPXY7djOegP7Ifd2%2Frg6ZWfabP5faBnQxiyHsHWLX7Al8jHc%3D|预订......"]},"httpstatus":200,"messages":"","status":true}
需要注意:
1.查询地址的queryZ会随机变化,有可能是queryA之类的,先随便传一个,如果不对的话 他会返回正的给你,再带上正确的地址去查询就可以了
{'c_name': 'CLeftTicketUrl', 'c_url': 'leftTicket/queryZ', 'status': False}
2.起点站终点站的数据是个简码,可以去下载他的信息下来自己整理 地址:https://www.12306.cn/index/script/core/common/station_name_v10015.js
是用@隔开的 这样↓自己解析成一个json文件 方便使用(自己写写)
=>
3.把查询的信息配置到一个方法中,方便读取更改
TicketDTO = {} # 封装请求参数
def initTicketDTO():
with open('./city.json', encoding='utf-8') as f:
CITY_DATA = json.load(f)
TicketDTO['train_date'] = '2019-02-14' #日期
TicketDTO['from_station_name'] = '重庆' #起点站
TicketDTO['to_station_name'] = '潼南' #终点站
TicketDTO['class'] = ['D5147'] #想购买车次 支持多个
TicketDTO['holder'] = ['XX'] #谁买票 目前只支持单人
TicketDTO['from_station'] = CITY_DATA[TicketDTO['from_station_name']] #起点站转换的简码
TicketDTO['to_station'] = CITY_DATA[TicketDTO['to_station_name']] #终点站简码
TicketDTO['passengerInfo'] = {} #购票人信息
查询车票代码:
select_ticket_URL = 'leftTicket/queryA' # 查票的地址是在queryA、queryZ啥的随机变化的
def select_ticket():
initTicketDTO()
global select_ticket_URL # 也可以从查询的初始化界面的leftTicket/init中获取,这就不写了 还是用这种方式
url = 'https://kyfw.12306.cn/otn/'+select_ticket_URL +\
'?leftTicketDTO.train_date='+TicketDTO['train_date'] + \
'&leftTicketDTO.from_station=' + TicketDTO['from_station'] +\
'&leftTicketDTO.to_station='+TicketDTO['to_station'] +\
'&purpose_codes=ADULT' # ADULT:单程普通票
data = get(url)
json_result = json.loads(data)
if(not json_result['status']):
select_ticket_URL = json_result['c_url']
select_ticket()
return
city_info = json_result['data']['map'] # 城市信息
for item in json_result['data']['result']:
classes = item.split('|') # 被竖线隔开的
canBuy = classes[11] # 是否可购买
secretStr = urllib.request.unquote(classes[0]) # 下单信息
IsEnable = (classes[0] == "" and False or True) # 有无预定信息
train_no = classes[2] # 班次
TrainClass = classes[3] # 班次
FirstSeat = classes[31] # 一等座
SecondSeat = Convert(classes[30]) # 二等座
print(canBuy+"=> 班次:"+TrainClass+" 历程:" + classes[13]+" "+city_info[classes[6]]+"=>"+city_info[classes[7]] + " "+classes[8]+"-" + classes[9]+"["+classes[10] + "h] 一等座:"+str(FirstSeat)+" 二等座:"+str(SecondSeat))
if(TrainClass in TicketDTO['class']):
print(train_no+":二等座余票:"+str(SecondSeat))
if(IsEnable and canBuy == 'Y'): # 有提交信息 并且可以购买
print('可购买,正在提交')
if(SecondSeat > 0):
while True:
json_initDc = submitOrderRequest(secretStr)
if(type(json_initDc) is dict):
checkOrderInfo(json_initDc)
time.sleep(2)
else:
print('不可购买')
def Convert(val):
if(val == '无' or val == ''):
return 0
elif(val == '有'): #有一般是大于20
return 20
return int(val)
返回的车次信息是通过’|’分割的 分割开来就可以看到了 大致有用的信息的元素位置代码中已经标注了
二、提交下单请求
地址:https://kyfw.12306.cn/otn/leftTicket/submitOrderRequest
参数:{
secretStr:NwCfMpWhQYGrv9zKy/E2bJHhqOoVHXTgo5hTboBBIe/Kzro2zxrnAiA4x1lswwh5upe5V31vZwXQuv7gxy............ # 这个参数是第一步查询的第一个参数,那趟车可买票的时候才会有这个参数
train_date:2019-02-14 # 发车日期
back_train_date:2019-01-20 # 返程日期(不是往返票不用管他)一般默认是当天
tour_flag:dc # 单程
purpose_codes:ADULT # 普通票
query_from_station_name:重庆 #起点站
query_to_station_name:潼南 # 终点站
undefined: # 为空的参数
}
返回:{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":"N","messages":[],"validateMessages":{}}
代码:
# 检测是否有未完成的订单,没有的话获取购票人
def submitOrderRequest(secretStr):
reqdata = {
"secretStr": secretStr,
"train_date": TicketDTO['train_date'],
"back_train_date": time.strftime('%Y-%m-%d', time.localtime(time.time())),
"tour_flag": 'dc',
"purpose_codes": 'ADULT',
"query_from_station_name": TicketDTO['from_station_name'],
"query_to_station_name": TicketDTO['to_station_name'],
"undefined": ''
}
url = 'https://kyfw.12306.cn/otn/leftTicket/submitOrderRequest'
data = post(url, reqdata)
json_result = json.loads(data)
if(json_result['status']):
if(json_result['data'] == 'N'): # 无未完成的订单 去获取参数然后获取购票人
json_initDc = getinitDc()
if(getPassenge(json_initDc['REPEAT_SUBMIT_TOKEN'])):
return json_initDc
else:
print(json_result['messages'])
return False
如果你有票未支付的状态下,会返回有未完成的订单什么什么的叫你去处理,其他没啥
三、获取一些参数后面用到
https://kyfw.12306.cn/otn/confirmPassenger/initDc #获取一堆参数 后面的请求需要用到
参数:{
_json_att:'' # 空参数
}
返回的是一个页面,需要用正则去取数据
代码:
def getinitDc():
html_data = post('https://kyfw.12306.cn/otn/confirmPassenger/initDc',
{"_json_att: ": ""})
REPEAT_SUBMIT_TOKEN = re.findall(re.compile(
"var globalRepeatSubmitToken = '(.*?)';", re.S), html_data)
key_check_isChange = re.findall(re.compile(
"'key_check_isChange':'(.*?)',", re.S), html_data)
leftTicketStr = re.findall(re.compile(
"'leftTicketStr':'(.*?)',", re.S), html_data)
tour_flag = re.findall(re.compile(
",'tour_flag':'(.*?)',", re.S), html_data)
purpose_codes = re.findall(re.compile(
",'purpose_codes':'(.*?)',", re.S), html_data)
train_location = re.findall(re.compile(
",'train_location':'(.*?)'", re.S), html_data)
train_no = re.findall(re.compile(",'train_no':'(.*?)',", re.S), html_data)
station_train_code = re.findall(re.compile(
",'station_train_code':'(.*?)',", re.S), html_data)
from_station_telecode = re.findall(re.compile(
",'from_station_telecode':'(.*?)',", re.S), html_data)
to_station = re.findall(re.compile(
",'to_station':'(.*?)',", re.S), html_data)
json_initDc = {
'REPEAT_SUBMIT_TOKEN': REPEAT_SUBMIT_TOKEN[0],
'key_check_isChange': key_check_isChange[0],
'leftTicketStr': leftTicketStr[0],
'tour_flag': tour_flag[0],
'purpose_codes': purpose_codes[0],
'train_location': train_location[0],
'train_no': train_no[0],
'station_train_code': station_train_code[0],
'from_station_telecode': from_station_telecode[0],
'to_station': to_station[0]
}
return json_initDc
四、获取购票人信息
https://kyfw.12306.cn/otn/confirmPassenger/getPassengerDTOs # 获取购票人信息 (乘车人信息)
参数:{
_json_att:'', #为空
REPEAT_SUBMIT_TOKEN:1e4811cbf86e722b50c7cb4293d1969c # 第三步中获取的一个参数
}
返回:{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{"notify_for_gat":"","isExist":true,"exMsg":"","two_isOpenClick":["93","95","97","99"],"other_isOpenClick":["91","93","98","99","95","97"],"normal_passengers":[{"code":"2","passenger_name":"XX","sex_code":"M","sex_name":"男","born_date":"199....","country_code":"CN","passenger_id_type_code":"1","passenger_id_type_name":"中国居民身份证","passenger_id_no":"500223....","passenger_type":"1","passenger_flag":"0","passenger_type_name":"成人","mobile_no":"13...","phone_no":"","email":"...@qq.com","address":"","postalcode":"","first_letter":"CZ","recordCount":"7","total_times":"99","index_id":"0","gat_born_date":"","gat_valid_date_start":"","gat_valid_date_end":"","gat_version":""},{"code":"1","passenger_name":"XX","sex_code":"M","sex_name":"男","born_date":"2016-02-14 00:00:00","country_code":"CN","passenger_id_type_code":"1","passenger_id_type_name":"中国居民身份证","passenger_id_no":"500230....","passenger_type":"1","passenger_flag":"0","passenger_type_name":"成人","mobile_no":"","phone_no":"","email":"","address":"","postalcode":"","first_letter":"AJ","recordCount":"7","total_times":"99","index_id":"1","gat_born_date":"","gat_valid_date_start":"","gat_valid_date_end":"","gat_version":""},{"code":"4","passenger_name":"XXX","sex_code":"F","sex_name":"女","born_date":"1900-01-01 00:00:00","country_code":"CN","passenger_id_type_code":"1","passenger_id_type_name":"中国居民身份证","passenger_id_no":"51160........","passenger_type":"1","passenger_flag":"0","passenger_type_name":"成人","mobile_no":"15.....","phone_no":"","email":"","address":"","postalcode":"","first_letter":"DYM","recordCount":"7","total_times":"99","index_id":"3","gat_born_date":"","gat_valid_date_start":"","gat_valid_date_end":"","gat_version":""}]},"messages":[],"validateMessages":{}}
代码
def getPassenge(REPEAT_SUBMIT_TOKEN):
if(any(TicketDTO['passengerInfo'])): #如果获取过了就直接返回
return True
else:
url = 'https://kyfw.12306.cn/otn/confirmPassenger/getPassengerDTOs'
reqdata = {
"_json_att: ": "",
"REPEAT_SUBMIT_TOKEN": REPEAT_SUBMIT_TOKEN[0]
}
data = post(url, reqdata)
json_result = json.loads(data)
for item in json_result['data']['normal_passengers']:
if item['passenger_name'] in TicketDTO['holder']:
TicketDTO['passengerInfo'] = {'passengerTicketStr': 'O,'+item['passenger_flag']+','+item['passenger_type']+','+item['passenger_name']+','+item['passenger_id_type_code']+','+item['passenger_id_no']+','+item['mobile_no']+',N',
'oldPassengerStr': item['passenger_name']+','+item['passenger_id_type_code']+','+item['passenger_id_no'] + ',1_'} # 拼接下面要用到的参数
if(any(TicketDTO['passengerInfo'])):
print("购票人数据获取成功")
return True
五、检查订单信息
https://kyfw.12306.cn/otn/confirmPassenger/checkOrderInfo # 检查订单信息 (是否能购票,是否有未支付的订单)
参数:{
cancel_flag:2 # 固定参数
bed_level_order_num:000000000000000000000000000000 # 固定参数
passengerTicketStr:O,0,1,XX,1,500223...,132...,N #购票人方法中拼接好的 购票人信息
oldPassengerStr:XX,1,5002.....,1_ #购票人中拼接好的
tour_flag:dc #单程。。基本固定的,不过第三步中获取过
randCode:'' #空参数
whatsSelect:1 # 固定
_json_att:'' #空参数
REPEAT_SUBMIT_TOKEN:1e4811cbf86e722b50c7cb4293d1969c #第三步中获取的
}
返回:{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{"ifShowPassCode":"N","canChooseBeds":"N","canChooseSeats":"Y","choose_Seats":"OM","isCanChooseMid":"N","ifShowPassCodeTime":"1","submitStatus":true,"smokeStr":""},"messages":[],"validateMessages":{}}
代码:
def checkOrderInfo(json_initDc):
reqdata = {
'cancel_flag': '2', #固定
'bed_level_order_num': '000000000000000000000000000000',
'passengerTicketStr': TicketDTO['passengerInfo']['passengerTicketStr'],
'oldPassengerStr': TicketDTO['passengerInfo']['oldPassengerStr'],
'tour_flag': json_initDc['tour_flag'], # 基本固定
'randCode': '',
'whatsSelect': '1', # 固定
'_json_att': '', # 为空
'REPEAT_SUBMIT_TOKEN': json_initDc['REPEAT_SUBMIT_TOKEN']
}
data = post(
'https://kyfw.12306.cn/otn/confirmPassenger/checkOrderInfo', reqdata)
json_result = json.loads(data)
print(json_result)
if(json_result['status']):
print('检查订单成功')
else:
print('检查订单失败'+json_result['messages'])
return
返回的可选座位类型之类的东西
6.获取余票与排队人数
https://kyfw.12306.cn/otn/confirmPassenger/getQueueCount # 获取余票,与排队人数
参数{
train_date:Thu+Feb+14+2019+00:00:00+GMT+0800+(中国标准时间) #购票时间格式化出来的
train_no:77000D514708 # 车次 第三步获取过
stationTrainCode:D5147 #班次 第三步获取过
seatType:O # O代表二等座,这儿有个固定字典的类型
fromStationTelecode:CUW #起点站简码 第三步获取过
toStationTelecode:TVW #终点站简码 第三步获取过
leftTicket:CLy0iYHVYp2Mctmf3%2FRbBmuCCUGyeyU8JF%2Bjolnv%2FfplIrD2 #第三步获取的
purpose_codes:00 #第三步获取的
train_location:W2 #第三步获取的
_json_att:'' #为空
REPEAT_SUBMIT_TOKEN:1e4811cbf86e722b50c7cb4293d1969c #第三步获取的
}
返回:{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{"count":"0","ticket":"463,85","op_2":"false","countT":"0","op_1":"false"},"messages":[],"validateMessages":{}}
代码:
# 获取余票
def getQueueCount():
reqdata = {
'train_date': time.strftime("%a+%b+%d+%Y+00:00:00+GMT+0800", time.strptime(TicketDTO['train_date'], "%Y-%m-%d"))+'+(中国标准时间)',
'train_no': json_initDc['train_no'], # 班次号
'stationTrainCode': json_initDc['station_train_code'], # 车次
'seatType': 'O',# json_result['data']['choose_Seats'], # 就是你选座的类型 二等座 O
'fromStationTelecode': json_initDc['from_station_telecode'], # 起点终点
'toStationTelecode': json_initDc['to_station'],
'leftTicket': json_initDc['leftTicketStr'],
'purpose_codes': json_initDc['purpose_codes'], # ?? 姑且认为是固定 没有取不到的参数!
'train_location': json_initDc['train_location'], # ??
'_json_att': '',
'REPEAT_SUBMIT_TOKEN': json_initDc['REPEAT_SUBMIT_TOKEN']
}
reqdata_Str= bytes.decode(urllib.parse.urlencode(reqdata).encode('utf-8')).replace('%2B','+').replace('GMT+0800','GMT%2B0800').replace('%28','(').replace('%29',')')
_headers= {
"Accept":"application/json, text/javascript, */*; q=0.01",
"Accept-Encoding":"gzip, deflate, br",
"Accept-Language":"zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",
"Host":"kyfw.12306.cn",
"Referer":"https://kyfw.12306.cn/otn/confirmPassenger/initDc",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0",
"X-Requested-With":"XMLHttpRequest"
}
data = post(
'https://kyfw.12306.cn/otn/confirmPassenger/getQueueCount',reqdata_Str,headers=_headers)
json_result = json.loads(data)
if(json_result['status']):
print(json_result)
print('提交下单信息成功,余票信息:'+json_result['data']['ticket']+' 排队人数:'+json_result['data']['count']) #T估计是票总数
if(json_result['data']['op_2']=='true'):
print('排队人数超过余票') # 应该需要重新发起请求之类的
else:
print('提交下单信息失败,'+json_result['messages'])
return
需注意:
1.train_date 参数格式化的问题,在这卡了1天…….,转码不对 提交的请求总会返回”url”:”/leftTicket/init”,”status”:false,”httpstatus”:200,”messages”:[“系统忙,请稍后重试”]
返回的信息里ticket代表票的余量 会返回2中票的余量 op_2为true时代表排队人数超过余票
六、选座,提交
https://kyfw.12306.cn/otn/confirmPassenger/confirmSingleForQueue
参数:{
passengerTicketStr:O,0,1,XX,1,500....,132.....,N # 拼接的购票人信息
oldPassengerStr:XX,1,5002....,1_ # 拼接的购票人信息
randCode:'' #空数据
purpose_codes:00 # 第三步获取
key_check_isChange:5EAB44C37C430901F5D2AA18462DD31BDADFE7DE17048A36906509CD # 第三步获取
leftTicketStr:Imh2yxCys%2Fo%2FhDX%2B3ZZOYgnkoNLrsZz6OFEYF5oGBrFRIB2p # 第三步获取
train_location:W2 # 第三步获取
choose_seats:1D #选的座位,没有的话他会帮你随机选。。
seatDetailType:000 # 固定
whatsSelect:1 # 固定
roomType:00 # 固定
dwAll:N # 固定
_json_att:'' #空
REPEAT_SUBMIT_TOKEN:1e4811cbf86e722b50c7cb4293d1969c # 第三步获取
}
返回:{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{"submitStatus":true},"messages":[],"validateMessages":{}}
代码:
def confirmSingleForQueue():
reqdata = {
'passengerTicketStr': TicketDTO['passengerInfo']['passengerTicketStr'],
'oldPassengerStr': TicketDTO['passengerInfo']['oldPassengerStr'],
'randCode': '',
'purpose_codes': json_initDc['purpose_codes'],
'key_check_isChange': json_initDc['key_check_isChange'],
'leftTicketStr': json_initDc['leftTicketStr'],
'train_location': json_initDc['train_location'],
'choose_seats': '1D', # ?? 选择的座位
'seatDetailType': '000', # 固定
'whatsSelect': '1',# 固定
'roomType': '00',# 固定
'dwAll': 'N',# 固定
'_json_att':'' ,# 固定
'REPEAT_SUBMIT_TOKEN': json_initDc['REPEAT_SUBMIT_TOKEN']
}
data = post(
'https://kyfw.12306.cn/otn/confirmPassenger/confirmSingleForQueue', reqdata)
print(data)
json_result=json.loads(data)
if(json_result['status']):
if(json_result['data']['submitStatus']):
print('选座成功!')
else:
print('选座失败。。 '+json_result['messages'])
return
七、提交订单 排队,获取订单id
https://kyfw.12306.cn/otn/confirmPassenger/queryOrderWaitTime?random=1547780170175&tourFlag=dc&_json_att=&REPEAT_SUBMIT_TOKEN=a8dc2ed1a814b3067cfc32550c17e0ed # 排队
参数:{
random1547780170175 #秒级时间戳
tourFlag:dc # 固定的 普通票
_json_att:''
REPEAT_SUBMIT_TOKEN:1e4811cbf86e722b50c7cb4293d1969c #第三步获取
}
返回:{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{"queryOrderWaitTimeStatus":true,"count":0,"waitTime":-1,"requestId":6491860556856379439,"waitCount":0,"tourFlag":"dc","orderId":"E702546392"},"messages":[],"validateMessages":{}}
代码:
# 下单等待确定
def queryOrderWaitTime():
orderId=''
OrderWait_url = 'https://kyfw.12306.cn/otn/confirmPassenger/queryOrderWaitTime?random='+str(int(time.time()*1000))+'&tourFlag=dc&_json_att=&REPEAT_SUBMIT_TOKEN='+json_initDc['REPEAT_SUBMIT_TOKEN']
html_data = get(OrderWait_url)
json_result = json.loads(html_data) #这边返回的订单ID 需要作为参数
print(json_result)
if(json_result['status'] and json_result['data']['queryOrderWaitTimeStatus']):
print('等待提交订单信息成功')
orderId=json_result['data']['orderId']
while(orderId==None):
time.sleep(json_result['data']['waitTime'])
html_data = get(OrderWait_url) #需要等一会才能返回订单的id
json_result = json.loads(html_data)
print(json_result)
if(json_result['status'] and json_result['data']['queryOrderWaitTimeStatus']):orderId=json_result['data']['orderId']
else:
print('等待提交订单信息失败,'+json_result['messages'])
return
需注意:返回的信息中有个等待时间waitTime代表需要等待这么久 才能获取到orderid 否则是none,orderid下面需要用到
八、确定回执信息
https://kyfw.12306.cn/otn/confirmPassenger/resultOrderForDcQueue # 确定下单成功
参数{
orderSequence_no:E702546390 #上一步返回的订单id
_json_att:'' #空
REPEAT_SUBMIT_TOKEN:1e4811cbf86e722b50c7cb4293d1969c #第三步的
}
返回:{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{"submitStatus":true},"messages":[],"validateMessages":{}}
代码:
# 回执信息
def resultOrderForDcQueue():
reqdata = {
'orderSequence_no':orderId,
'_json_att':'',
'REPEAT_SUBMIT_TOKEN': json_initDc['REPEAT_SUBMIT_TOKEN']
}
data = post(
'https://kyfw.12306.cn/otn/confirmPassenger/resultOrderForDcQueue', reqdata)
print(data)
json_result = json.loads(data)
if(json_result['status']):
if(json_result['data']['submitStatus']):
print('下单成功!!!!!!!!!去付款吧')
# Email提醒
else:
print('下单失败。。。。。。。 '+json_result['messages'])
五-八我是写在一个方法里的,这里为了展示,分出来
注意:
“submitStatus”:true !!!!说明下单成功了,去12306里支付就ok!!
源码地址:https://github.com/YuChenDayCode/Ticket
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/26059.html