12306余票照会(三)——余票運賃を取得する


           ,     ,            ,  ,         。          ,  ,     ,       ,       ,               
            ,            
def get_price(train_no, from_station_no, destinction_no, seat_types, date):
    base_url = "https://kyfw.12306.cn/otn/leftTicket/queryTicketPrice?"
    parmas = {
        "train_no": train_no,
        "from_station_no": from_station_no,
        "to_station_no": destinction_no,
        "seat_types": seat_types,
        "train_date": date
    }
    url = base_url + urlencode(parmas)
    try:
        data = requests.get(url).text
    except Exception as e:
        print("      "+"|"+e+"|"+url) 
    data = json.loads(data)
    data_dic = data["data"]
    price_dic = {
        "business_price": "--",
        "first_seat_price": "--",
        "second_seat_price": "--",
        "gjrw_seat_price": "--",
        "rw_seat_price": "--",
        "dw_seat_price": "--",
        "yw_seat_price": "--",
        "rz_seat_price": "--",
        "yz_seat_price": "--",
        "wz_seat_price": "--",
        "qt_seat_price": "--"
    }
    #      
    if("A9" in data_dic.keys()):
        price_dic["business_price"] = data_dic["A9"]
    elif("p" in data_dic.keys()):
        price_dic["business_price"] = data_dic["p"]
    #    
    if("M" in data_dic.keys()):
        price_dic["first_seat_price"] = data_dic["M"]
    #    
    if("O" in data_dic.keys()):
        price_dic["second_seat_price"] = data_dic["O"]
    #     
    if("A6" in data_dic.keys()):
        price_dic["gjrw_seat_price"] = data_dic["A6"]
    #   
    if("A4" in data_dic.keys()):
        price_dic["rw_seat_price"] = data_dic["A4"]
    #   
    if("F" in data_dic.keys()):
        price_dic["dw_seat_price"] = data_dic["F"]
    #   
    if("A3" in data_dic.keys()):
        price_dic["yw_seat_price"] = data_dic["A3"]
    #   
    if("A2" in data_dic.keys()):
        price_dic["rz_seat_price"] = data_dic["A2"]
    #   
    if("A1" in data_dic.keys()):
        price_dic["yz_seat_price"] = data_dic["A1"]
    #   
    if("WZ" in data_dic.keys()):
        price_dic["wz_seat_price"] = data_dic["WZ"]
    return price_dic

残票情報の取得要求と非常に類似しており、分析データも類似しており、ここでは関数パラメータはあまり言わず、tickets_に格納されています.infoの次の節では、チケットの取得で得られた情報と余剰チケットの情報をどのように結びつけるかについて説明します.