携程机票查询

 比较简单,直接放代码,代码仅供学习。

import requests, re, json, time, random

city_listed = {"阿勒泰": "AAT", "兴义": "ACX", "百色": "AEB", "阿克苏": "AKU", "鞍山": "AOG", "安庆": "AQG", "安顺": "AVA",
               "阿拉善左旗": "AXF", "包头": "BAV", "毕节": "BFJ", "北海": "BHY", "北京": "BJS", "秦皇岛": "BPE", "博乐": "BPL",
               "昌都": "BPX", "保山": "BSD", "广州": "CAN", "承德": "CDE", "常德": "CGD", "郑州": "CGO", "长春": "CGQ", "朝阳": "CHG",
               "赤峰": "CIF", "长治": "CIH", "重庆": "CKG", "长沙": "CSX", "成都": "CTU", "沧源": "CWJ", "嘉义": "CYI", "常州": "CZX",
               "大同": "DAT", "达县": "DAX", "白城": "DBC", "稻城": "DCY", "丹东": "DDG",
               "香格里拉(迪庆)": "DIG", "大连": "DLC", "大理": "DLU", "敦煌": "DNH", "东营": "DOY", "大庆": "DQA", "鄂尔多斯": "DSN",
               "张家界": "DYG", "额济纳旗": "EJN", "恩施": "ENH",
               "延安": "ENY", "二连浩特": "ERL", "福州": "FOC", "阜阳": "FUG", "佛山": "FUO", "抚远": "FYJ", "格尔木": "GOQ",
               "广元": "GYS", "固原": "GYU", "海口": "HAK", "邯郸": "HDG",
               "黑河": "HEK", "呼和浩特": "HET", "合肥": "HFE", "杭州": "HGH", "淮安": "HIA", "怀化": "HJJ", "香港": "HKG",
               "海拉尔": "HLD", "乌兰浩特": "HLH", "哈密": "HMI", "神农架": "HPG",
               "哈尔滨": "HRB", "舟山": "HSN", "和田": "HTN", "惠州": "HUZ", "台州": "HYN", "汉中": "HZG", "黎平": "HZH", "银川": "INC",
               "且末": "IQM", "庆阳": "IQN", "景德镇": "JDZ",
               "加格达奇": "JGD", "嘉峪关": "JGN", "井冈山": "JGS", "西双版纳": "JHG", "金昌": "JIC", "黔江": "JIQ", "九江": "JIU",
               "晋江": "JJN", "澜沧": "JMJ", "佳木斯": "JMU", "济宁": "JNG",
               "锦州": "JNZ", "建三江": "JSJ", "池州": "JUH", "衢州": "JUZ", "鸡西": "JXA", "九寨沟": "JZH", "库车": "KCA", "康定": "KGT",
               "喀什": "KHG", "南昌": "KHN", "凯里": "KJH", "昆明": "KMG",
               "金门": "KNH", "赣州": "KOW", "库尔勒": "KRL", "克拉玛依": "KRY", "贵阳": "KWE", "桂林": "KWL", "龙岩": "LCX",
               "伊春": "LDS", "临汾": "LFQ", "兰州": "LHW", "丽江": "LJG", "荔波": "LLB",
               "永州": "LLF", "吕梁": "LLV", "临沧": "LNJ", "六盘水": "LPF", "芒市": "LUM", "拉萨": "LXA", "洛阳": "LYA", "连云港": "LYG",
               "临沂": "LYI", "柳州": "LZH", "泸州": "LZO",
               "林芝": "LZY", "牡丹江": "MDG", "马祖": "MFK", "澳门": "MFM", "绵阳": "MIG", "梅州": "MXZ", "南充": "NAO", "白山": "NBS",
               "齐齐哈尔": "NDG", "宁波": "NGB", "阿里": "NGQ",
               "南京": "NKG", "宁蒗": "NLH", "南宁": "NNG", "南阳": "NNY", "南通": "NTG", "满洲里": "NZH", "漠河": "OHE", "攀枝花": "PZI",
               "阿拉善右旗": "RHT", "日照": "RIZ", "日喀则": "RKZ",
               "巴彦淖尔": "RLK", "上海": "SHA", "沈阳": "SHE", "西安": "SIA", "石家庄": "SJW", "揭阳": "SWA", "普洱": "SYM",
               "三亚": "SYX", "深圳": "SZX", "青岛": "TAO", "塔城": "TCG", "腾冲": "TCZ",
               "铜仁": "TEN", "通辽": "TGO", "天水": "THQ", "吐鲁番": "TLQ", "济南": "TNA", "天津": "TSN", "唐山": "TVS", "黄山": "TXN",
               "太原": "TYN", "乌鲁木齐": "URC", "榆林": "UYN", "潍坊": "WEF",
               "威海": "WEH", "遵义(茅台)": "WMT", "文山": "WNH", "温州": "WNZ", "乌海": "WUA", "武汉": "WUH", "武夷山": "WUS",
               "无锡": "WUX", "梧州": "WUZ", "万州": "WXN", "襄阳": "XFN", "西昌": "XIC",
               "锡林浩特": "XIL", "厦门": "XMN", "西宁": "XNN", "徐州": "XUZ", "宜宾": "YBP", "运城": "YCU", "宜春": "YIC",
               "阿尔山": "YIE", "宜昌": "YIH", "伊宁": "YIN", "义乌": "YIW", "延吉": "YNJ",
               "烟台": "YNT", "盐城": "YNZ", "扬州": "YTY", "玉树": "YUS", "张掖": "YZY", "昭通": "ZAT", "湛江": "ZHA", "中卫": "ZHY",
               "张家口": "ZQZ", "珠海": "ZUH", "遵义(新舟)": "ZYI", }


def run():
    url = "https://flights.ctrip.com/itinerary/api/12808/products"

    headers = {
        'accept': '*/*',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'content-type': 'application/json',
        # 不放cookie 的话 数据有一点点不同,测试了几组数据 还不确定数据是不是完全正确
        'cookie': '_RGUID=5b116341-bf9a-4847-9e3c-3c5e1891ee3e; _RSG=7Xac_6qOHi5.uUaOu5Fyu9; _RDG=283451d6944b5f22620e40dce901762019; _abtest_userid=9d627d21-5e7c-4ecb-876f-50b27a689ce3',
        'origin': 'https://flights.ctrip.com',
        'referer': 'https://flights.ctrip.com/itinerary/oneway/sha-wuh?date=2019-03-15&mkt_header=bdnm',
        'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
    }
    json = {
        "flightWay": "Oneway",  # 飞行方式 单向的 固定参数
        "classType": "ALL",
        "hasChild": "false",
        "hasBaby": "false",
        "searchIndex": 1,
        "mkt_header": "bdnm",  # 固定参数
        "airportParams": [
            {
                "dcity": "SHA",  # 起点
                "acity": "NKG",  # 终点
                "dcityname": "上海",  # 起点名称 没有起作用
                "acityname": "杭州",  # 终点名称 设置错误 暂时也是返回的正确数据
                "date": "2019-03-16"  # 请求时间
            }
        ]
    }

    response = requests.post(url=url, headers=headers, json=json, verify=False, timeout=15)

    data = response.text

    # with open("搜索结果16.json", "w", encoding="utf-8") as f:
    #     f.write(data)
    analysis(data)

def analysis(data):
    # with open("搜索结果16.json", "r", encoding="utf-8") as f:
    #     data = f.read()
    data_json = eval(data.replace("null", "None").replace("false", "False").replace("true", "True"))
    route_List = data_json["data"]["routeList"]
    for route in route_List:
        legs = route["legs"][0]
        flight = legs["flight"]
        flightNumber = flight["flightNumber"]  # 飞机编号
        craftTypeName = flight["craftTypeName"]  # 飞机大小类型
        punctualityRate = flight["punctualityRate"]  # 准点率
        departureDate = flight["departureDate"]  # 出发时间
        arrivalDate = flight["arrivalDate"]  # 准点到达时间
        start_info = flight["departureAirportInfo"]["airportName"]  # 出发机场
        arrive_info = flight["arrivalAirportInfo"]["airportName"]  # 出发机场

        flightId = legs["flightId"]  # 飞行ID
        cabins_list = legs["cabins"]
        print(str(flightNumber) + " 准点率:" + punctualityRate, end=" : ")
        price_min = 10000
        for cabins in cabins_list:
            price = cabins["price"]  # 价格对象
            seatCount = cabins["seatCount"]  # 该价格座位数量 等于10 应该是 大于等于10
            price_original = price["price"]  # price_original salePrice printPrice 显示的价格好像是一样的
            salePrice = price["salePrice"]
            printPrice = price["printPrice"]  # 显示价格

            fdPrice = price["fdPrice"]  # 这个可能比显示的价格高或低
            rate = price["rate"]  # 折扣率

            if price_min > printPrice:
                price_min = printPrice
        print("最低价: ", price_min)
        print("\t出发点:" + start_info + "\t到达地: " + arrive_info)
        print("\t出发时间:" + departureDate + "\t到达时间: " + arrivalDate)


if __name__ == '__main__':
    run()


 

你可能感兴趣的:(爬虫)