45.预定订单初始化、解析用户信息以及坐席信息
2020-08-28 本文已影响0人
M_小七
预定订单初始化、解析用户信息以及坐席信息
学习目标
- 了解 预定订单初始化
- 了解 解析构造用户信息
- 了解 解析构造坐席信息
4.1 预定订单初始化
# 12306.funk12306.Funk12306.buy_ticket
# 检查用户是否保持登录成功
url = 'https://kyfw.12306.cn/otn/login/checkUser'
data = {'_json_att': ''}
resp = self.s.post(url, data=data)
print(json.loads(resp.text))
# 点击预定
url = 'https://kyfw.12306.cn/otn/leftTicket/submitOrderRequest'
data = {
'secretStr': secretStr,
'train_date': train_date,
'back_train_date': train_date,
'tour_flag': 'dc', # dc 单程 wf 往返
'purpose_codes': 'ADULT', # 成人
'query_from_station_name': from_station,
'query_to_station_name': to_station,
'undefined': ''
}
resp = self.s.post(url, data=data)
print(resp.text)
# 订单初始化 获取REPEAT_SUBMIT_TOKEN key_check_isChange
url = 'https://kyfw.12306.cn/otn/confirmPassenger/initDc'
data = {'_json_att': ''}
response = self.s.post(url, data=data)
repeat_submit_token = re.search(r"var globalRepeatSubmitToken = '([a-z0-9]+)';",
response.content.decode()).group(1)
key_check_isChange = re.search("'key_check_isChange':'([A-Z0-9]+)'", response.content.decode()).group(1)
4.2 获取用户信息
# 12306.funk12306.Funk12306.buy_ticket
# 获取用户信息
# 需要 REPEAT_SUBMIT_TOKEN
url = 'https://kyfw.12306.cn/otn/confirmPassenger/getPassengerDTOs'
data = {'_json_att': '',
'REPEAT_SUBMIT_TOKEN': repeat_submit_token}
response = self.s.post(url, data=data)
4.3 解析用户信息
# 12306.utils.parse_passenger
def parsePassenger(passenger_dict):
passengers_infos_list = passenger_dict['data']['normal_passengers']
passenger_list = []
for passenger_info in passengers_infos_list:
passenger_info_dict = {}
passenger_info_dict['passenger_name'] = passenger_info.get('passenger_name', '')
passenger_info_dict['passenger_gender'] = passenger_info.get('sex_name', '')
passenger_info_dict['passenger_id_type_code'] = passenger_info.get('passenger_id_type_code', '')
passenger_info_dict['passenger_id_no'] = passenger_info.get('passenger_id_no', '')
passenger_info_dict['passenger_mobile_no'] = passenger_info.get('mobile_no', '')
passenger_list.append(passenger_info_dict)
return passenger_list
4.4 获取乘客信息列表
# 12306.funk12306.Funk12306.buy_ticket
# 解析并构造乘客信息列表
passenger_list = parsePassenger(json.loads(response.content))
print('获取乘客信息有:')
pprint(passenger_list)
passenger_info_dict = passenger_list[int(input('输入要购票的乘车人的下标'))]
4.5 解析坐席类型
经过不断的抓包,尝试获取坐席类型的编码
# 12306.utils.parse_seat_type
seat_type_dict = {
"erdengzuo": "O", # 二等座
"yingwo": "3", # 硬卧
"yingzuo": "1", # 硬座
"wuzuo": "1", # 无座
"ruanwo": "4", # 软卧
"ruanzuo": "2", # 软座
"dongwo": "F", # 动卧
"yidengzuo": "M", # 一等座
"gaojiruanwo": "6", # 高级软座
"shangwuzuo": "9", # 商务座
"tedengzuo": "P", # 特等座
}
4.6 构造乘客信息
# 12306.funk12306.Funk12306.buy_ticket
# 坐席类型
try:
seat_type = seat_type_dict[input('请输入要购买的坐席类型的拼音,如果输入错误,将强行购买无座,能回家就行了,还要tm什么自行车!:')]
except:
seat_type = seat_type_dict['wuzuo']
# 构造乘客信息
passengerTicketStr = '%s,0,1,%s,%s,%s,%s,N' % (
seat_type, passenger_info_dict['passenger_name'],
passenger_info_dict['passenger_id_type_code'],
passenger_info_dict['passenger_id_no'],
passenger_info_dict['passenger_mobile_no'])
oldPassengerStr = '%s,%s,%s,1_' % (
passenger_info_dict['passenger_name'],
passenger_info_dict['passenger_id_type_code'],
passenger_info_dict['passenger_id_no'])
# 检查选票人信息
url = 'https://kyfw.12306.cn/otn/confirmPassenger/checkOrderInfo'
data = {
'cancel_flag': '2', # 未知
'bed_level_order_num': '000000000000000000000000000000', # 未知
'passengerTicketStr': passengerTicketStr.encode('utf-8'), # O,0,1,靳文强,1,142303199512240614,18335456020,N
'oldPassengerStr': oldPassengerStr.encode('utf-8'), # 靳文强,1,142303199512240614,1_
'tour_flag': 'dc', # 单程
'randCode': '',
'whatsSelect': '1',
'_json_att': '',
'REPEAT_SUBMIT_TOKEN': repeat_submit_token
}
resp = self.s.post(url, data=data)
print(resp.text)
小结
- 了解 预定订单初始化
- 了解 解析构造用户信息
- 了解 解析构造坐席信息