自己做的一点点抓包流程总结
具体代码可以参考我的github:https://github.com/favoMJ/12306
登录:略
1 取得参数:
_from ,_to 初始地,目的地 对应编号
d,日期
2 设置cookies:
from_station = quote(self.get_code_by_input(_from) + ',' + _from).replace('\\', '%')
to_station = quote(self.get_code_by_input(_to) + ',' + _to).replace('\\', '%')
取得_from , _to汉字,转换为url编码
cookies = {
"_jc_save_fromDate": d,
"_jc_save_fromStation": from_station,
"_jc_save_toDate": d,
"_jc_save_toStation": to_station,
"_jc_save_wfdc_flag": "dc",
}
requests.utils.add_dict_to_cookiejar(self.session.cookies, cookies)
通过requests.utils.add_dict_to_cookiejar对session对象设置cookie,之后所有的请求都会自动加上我自定义的cookie内容。
3 查看车票信息:
url = 'https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=' + d + '&leftTicketDTO.from_station=' \
+ _from + '&leftTicketDTO.to_station=' + _to + '&purpose_codes=ADULT'
4 生成json , 并选择一个:
self.current_train =
[
{
"secretStr": "wzmB%2BcoEpgessBl5X2R2KQ9z6xPphVCaZzp4AewCONRO2ocouQXyCl8h130S4MRHk962xYSIoyUu%0AMb%2F6cJzsQBbwnYMcIEbgDALAIiyUmsgPuQCibbQR0wjUE4LBaFwQdvYu4aYkHYAXbZNSQomTXmjn%0AO0x4RSdci8DF1ITJwmYYxCO9R5JgLKxdVgJYGGxqxo9JeLqiKFmTj8euOM2PXX2168Ih7NqdI5Zd%0AnYiT2fOM7pWF2BlZ%2FQ%3D%3D",
"一等座": "",
"二等座": "",
"其它": "",
"出发日": "20171130",
"出发时间": "22:16",
"出发站": "深圳西",
"到达时间": "03:41",
"到达站": "铜仁",
"动卧": "",
"历时": "05:25",
"商务座特等座": "",
"当日到达": "Y",
"无座": "有",
"硬卧": "有",
"硬座": "有",
"车次": "K9064",
"软卧": "20",
"软座": "",
"高级软卧": ""
}
]
5 提交预订单:
data = {
"secretStr": unquote(self.current_train['secretStr']),
"train_date": train_date,
"back_train_date": back_date,
"tour_flag": "dc",
"purpose_codes": "ADULT",
"query_from_station_name": _f,
"query_to_station_name": _t,
"undefined": None
}
5.1 检查用户是否存在:
url = 'https://kyfw.12306.cn/otn/login/checkUser'
data = {'_json_att': ''}
res = self.session.post(url,
data=data,
verify=False,
headers={'If-Modified-Since': '0',
'Cache-Control': 'no-cache',
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'})
5.2 提交预订单
url = 'https://kyfw.12306.cn/otn/leftTicket/submitOrderRequest'
res = self.session.post(url,
data=data,
headers={"Content-Type": "application/x-www-form-urlencoded"},
verify=False)
6 确认乘客信息
url = 'https://kyfw.12306.cn/otn/confirmPassenger/initDc'
res = self.session.get(url, verify=False)
通过返回页面获得信息,比如token,列车信息
获取乘客信息
url = 'https://kyfw.12306.cn/otn/confirmPassenger/getPassengerDTOs'
data = {
'_json_att': '',
'REPEAT_SUBMIT_TOKEN': token
}
选择乘客
根据列车信息+乘客信息获得参数
7 验证订单
url = 'https://kyfw.12306.cn/otn/confirmPassenger/checkOrderInfo'
data = {
'cancel_flag': '2',
'bed_level_order_num': '000000000000000000000000000000',
'passengerTicketStr': self.get_passenger_tickets(),
'oldPassengerStr': self.get_old_passengers(),
'tour_flag': self.ticketInfoForPassengerForm['tour_flag'],
'randCode': '',
'_json_att': '',
'REPEAT_SUBMIT_TOKEN': token
}
res = self.session.post(url, data=data, verify=False)
8 获取队列参数
url = 'https://kyfw.12306.cn/otn/confirmPassenger/getQueueCount'
data = {
'train_date': self.get_standard_time(
datetime.datetime.fromtimestamp(int(self.orderRequestDTO['train_date']['time'])/1000)),
'train_no': self.orderRequestDTO['train_no'],
'stationTrainCode': self.orderRequestDTO['station_train_code'],
'seatType': self.limit_tickets[0]['seat_type'],
'fromStationTelecode': self.orderRequestDTO['from_station_telecode'],
'toStationTelecoe': self.orderRequestDTO['to_station_telecode'],
'leftTicket': self.ticketInfoForPassengerForm['queryLeftTicketRequestDTO']['ypInfoDetail'],
'purpose_codes': self.ticketInfoForPassengerForm['purpose_codes'],
'train_location': self.ticketInfoForPassengerForm['train_location'],
'_json_att': '',
'REPEAT_SUBMIT_TOKEN': token
}
res = self.session.post(url, data=data, verify=False)
9 确认队列单
url = 'https://kyfw.12306.cn/otn/confirmPassenger/confirmSingleForQueue'
data = {
'passengerTicketStr': self.get_passenger_tickets(),
'oldPassengerStr': self.get_old_passengers(),
'randCode': '',
'purpose_codes': self.ticketInfoForPassengerForm['purpose_codes'],
'key_check_isChange': self.ticketInfoForPassengerForm['key_check_isChange'],
'leftTicketStr': self.ticketInfoForPassengerForm['leftTicketStr'],
'train_location': self.ticketInfoForPassengerForm['train_location'],
'choose_seats': '',
'seatDetailType': '000',
'roomType': '00',
'dwAll': 'N',
'_json_att': '',
'REPEAT_SUBMIT_TOKEN': token
}
res = self.session.post(url, data=data, verify=False)
10 获取下次请求等待时间
url = 'https://kyfw.12306.cn/otn/confirmPassenger/queryOrderWaitTime?random=' + t\
+ '&tourFlag=' + self.ticketInfoForPassengerForm['tour_flag'] + '&_json_att=&REPEAT_SUBMIT_TOKEN=' + token
res = self.session.get(url, verify=False)
11 订单完成
if tour_flag == self.ticket_submit_order['tour_flag']['dc']:
url = 'https://kyfw.12306.cn/otn/confirmPassenger/resultOrderForDcQueue'
elif tour_flag == self.ticket_submit_order['tour_flag']['wc']:
url = 'https://kyfw.12306.cn/otn/confirmPassenger/resultOrderForWcQueue'
elif tour_flag == self.ticket_submit_order['tour_flag']['fc']:
url = 'https://kyfw.12306.cn/otn/confirmPassenger/resultOrderForFcQueue'
elif tour_flag == self.ticket_submit_order['tour_flag']['gc']:
url = 'https://kyfw.12306.cn/otn/confirmPassenger/resultOrderForGcQueue'
data = {
'orderSequence_no': wait_obj['orderId'],
'_json_att': '',
'REPEAT_SUBMIT_TOKEN': token
}
res = self.session.post(url, data=data, verify=False)
12 跳转至已订得车票页面
url = 'https://kyfw.12306.cn/otn//payOrder/init?random=' + t
res = self.session.post(url, verify=False)