接口
1、檢查用戶是否保持登錄成功
https://kyfw.12306.cn/otn/login/checkUser
2、點擊預定
https://kyfw.12306.cn/otn/leftTicket/submitOrderRequest
3、獲取聯系人
https://kyfw.12306.cn/otn/confirmPassenger/getPassengerDTOs
4、檢查選票人信息
https://kyfw.12306.cn/otn/confirmPassenger/checkOrderInfo
5、提交訂單
https://kyfw.12306.cn/otn/confirmPassenger/getQueueCount
6、確認訂單
https://kyfw.12306.cn/otn/confirmPassenger/confirmSingleForQueue
7、排隊等待
https://kyfw.12306.cn/otn/confirmPassenger/queryOrderWaitTime
8、訂單結果
https://kyfw.12306.cn/otn/confirmPassenger/resultOrderForDcQueue
說明
需要做的依次post服務器需要的參數后,根據response返回的內容進行判斷是否成功。
1. checkUser

post參數_json_att為空,注意返回的json,flag:true 為保持登錄成功
2. 點擊預定

post參數 secretStr 根據查看返回的預定號urldecode轉碼而來,python3 可使用urllib.parse.unquote來進行轉碼,train_date 出發時間,back_train_date 返程時間 , tour_flag 固定值。
3. 獲取聯系人

post的參數_json_att為空,REPEAT_SUBMIT_TOKEN在上一次請求頁面的源碼中。由於下面截圖是后續截的,所以與上一次不同,由此可見每一次的REPEAT_SUBMIT_TOKEN都會改變,所以只要將https://kyfw.12306.cn/otn/confirmPassenger/initDc get下來,使用正則便可以分析出來。方便后續使用。獲取的聯系人可以將有用的信息,買票人信息提取出來,為后面選票人下單提供參數。

4. 檢查選票人信息

'cancel_flag':'2', #固定值 'bed_level_order_num':'000000000000000000000000000000', #固定值 'passengerTicketStr':'3,0,1,xxx,1,xxxxxxxxxxxxxx,,N', #座位類型,0,票類型(成人/兒童),name,身份類型(身份證/軍官證….),身份證,電話號碼,保存狀態 'oldPassengerStr':'xxx,1,xxxxxxxxxxxxxxxx,1', #姓名 1 身份證號碼 1 'tour_flag':'dc', #固定值 'randCode':'', #隨機數 'whatsSelect':'1', '_json_att':'', #空 'REPEAT_SUBMIT_TOKEN': xxxxxxxxx #上面獲取過
5. 提交訂單

data={ 'train_date': 'Wed Apr 22 2018 00:00: 00 GMT + 0800', #出發時間,注意格式 "REPEAT_SUBMIT_TOKEN": xxxxxxxx, #上面獲取過 "_json_att": "", #空 "fromStationTelecode": fromStationTelecode, #出發站的英文編號 "leftTicket": leftTicket, #和REPEAT_SUBMIT_TOKEN一樣在相同網頁獲取 "purpose_codes": purpose_codes, #和REPEAT_SUBMIT_TOKEN一樣在相同網頁獲取 "seatType": '3', #3為硬卧,具體可以查看https://kyfw.12306.cn/otn/confirmPassenger/initDc 檢視元素可查看 "stationTrainCode": station_train_code, #車次 "toStationTelecode": to_station_telecode, #目的站的也穩編號 "train_no": train_no, #和REPEAT_SUBMIT_TOKEN一樣在相同網頁獲取 'train_location':trainl_lst[2] #車次序列號,和REPEAT_SUBMIT_TOKEN一樣在相同網頁獲取 }
6. 確認訂單

data={ 'passengerTicketStr': '3,0,1,xxx,1,xxxxxxxxxx,,N', #選票人信息,獲取過 'oldPassengerStr': 'xxx,1,xxxxxxxxxxxxxxx,1_', #獲取過 'randCode': '', #隨機值,空 "purpose_codes": purpose_codes, #獲取過 "key_check_isChange": key_check_isChange, #和REPEAT_SUBMIT_TOKEN一樣在相同網頁獲取 "leftTicketStr": leftTicket, #獲取過 'train_location': trainl_lst[2], #獲取過 'choose_seats':'', #座位類型,一般是高鐵用 'roomType': '00', #固定值 'dwAll': 'N', #固定值 "_json_att": "", #空 'seatDetailType':'000', #固定值 'whatsSelect': '1', #固定值 "REPEAT_SUBMIT_TOKEN": ticketToken, #獲取過 }
7. 排隊等待
第一次post:

第二次post:

第一次和第二次post參數相同,但是第二次返回json中多了一個orderid,在訂單結果中使用。
data={ "REPEAT_SUBMIT_TOKEN": ticketToken, #獲取過 "_json_att": "", #空 "random": str(time.time()), #隨機值 "tourFlag": "dc" #固定值 }
8. 訂單結果

data={ "REPEAT_SUBMIT_TOKEN": ticketToken, #獲取過 "_json_att": "", #空 "orderSequence_no": orderId #獲取到排隊等待response中的orderid }
