代码之家  ›  专栏  ›  技术社区  ›  Dallin Kump

通过格式化JSON过滤API调用的结果

  •  1
  • Dallin Kump  · 技术社区  · 2 年前

    我有下面的代码,它发出一个API调用,返回市场上每个列表的信息。为了隐私,我已经删除了clientId和clientSecret。

    """
    import base64
    import requests
    
    
    clientId = "fakeId"
    clientSecret = "fakeSecret"
    clientData = f"{clientId}:{clientSecret}"
    encodedData = str(base64.b64encode(clientData.encode("utf-8")), "utf-8")
    authorizationHeaderString = f"Basic {encodedData}"
    
    import requests
    
    r = requests.get("https://api.skinport.com/v1/items", params={
        "app_id": 730,
        "currency": "EUR",
        "tradable": 0
    }).json()
    
    print(r)
    """
    

    以下是输出中的一个示例:

    """
    {'market_hash_name': 'USP-S | Whiteout (Factory New)', 'currency': 'EUR', 'suggested_price': 153.1, 'item_page': 'https://skinport.com/item/usp-s-whiteout-factory-new', 'market_page': 'https://skinport.com/market?item=Whiteout&cat=Pistol&type=USP-S', 'min_price': 171.03, 'max_price': 232.16, 'mean_price': 197.13, 'median_price': 194.25, 'quantity': 7, 'created_at': 1633294586, 'updated_at': 1693686845}, {'market_hash_name': 'USP-S | Whiteout (Field-Tested)', 'currency': 'EUR', 'suggested_price': 22.18, 'item_page': 'https://skinport.com/item/usp-s-whiteout-field-tested', 'market_page': 'https://skinport.com/market?item=Whiteout&cat=Pistol&type=USP-S', 'min_price': 19.25, 'max_price': 175, 'mean_price': 41.53, 'median_price': 36.55, 'quantity': 61, 'created_at': 1632899292, 'updated_at': 1693686845}, {'market_hash_name': 'USP-S | Whiteout (Minimal Wear)', 'currency': 'EUR', 'suggested_price': 32.96, 'item_page': 'https://skinport.com/item/usp-s-whiteout-minimal-wear', 'market_page': 'https://skinport.com/market?item=Whiteout&cat=Pistol&type=USP-S', 'min_price': 39.64, 'max_price': 185, 'mean_price': 77.81, 'median_price': 69.99, 'quantity': 61, 'created_at': 1632921139, 'updated_at': 1693686845}, {'market_hash_name': 'USP-S | Whiteout (Well-Worn)', 'currency': 'EUR', 'suggested_price': 17.62, 'item_page': 'https://skinport.com/item/usp-s-whiteout-well-worn', 'market_page': 'https://skinport.com/market?item=Whiteout&cat=Pistol&type=USP-S', 'min_price': 19.79, 'max_price': 32, 'mean_price': 25.9, 'median_price': 25.9, 'quantity': 2, 'created_at': 1632976633, 'updated_at': 1693686845}, {'market_hash_name': 'Valeria Phoenix Pin', 'currency': 'EUR', 'suggested_price': 51.74, 'item_page': 'https://skinport.com/item/valeria-phoenix-pin', 'market_page': 'https://skinport.com/market?item=Valeria%20Phoenix%20Pin&cat=Collectible', 'min_price': 53.95, 'max_price': 80, 'mean_price': 63.66, 'median_price': 59.99, 'quantity': 14, 'created_at': 1535988295, 'updated_at': 1693686845}, 
    """
    

    我对JSON格式相对不熟悉,所以我的问题是,过滤返回数据的最佳方式是什么,这样我就可以存储满足某些条件的项目的信息。例如,该程序返回了成千上万行关于列表的信息,但我只关心某个market_hash_name或某个min_price的列表,或者两者兼而有之。

    我确信有一种方法可以通过编辑API调用来实现这一点,但我更喜欢通过简单地格式化返回的JSON来实现。

    谢谢你的帮助!

    1 回复  |  直到 2 年前
        1
  •  0
  •   Andrej Kesely    2 年前

    你可以使用列表理解来过滤你的结果。In变量 r 你有(我假设)字典列表:

    r = [
        {
            "market_hash_name": "USP-S | Whiteout (Factory New)",
            "currency": "EUR",
            "suggested_price": 153.1,
            "item_page": "https://skinport.com/item/usp-s-whiteout-factory-new",
            "market_page": "https://skinport.com/market?item=Whiteout&cat=Pistol&type=USP-S",
            "min_price": 171.03,
            "max_price": 232.16,
            "mean_price": 197.13,
            "median_price": 194.25,
            "quantity": 7,
            "created_at": 1633294586,
            "updated_at": 1693686845,
        },
        {
            "market_hash_name": "USP-S | Whiteout (Field-Tested)",
            "currency": "EUR",
            "suggested_price": 22.18,
            "item_page": "https://skinport.com/item/usp-s-whiteout-field-tested",
            "market_page": "https://skinport.com/market?item=Whiteout&cat=Pistol&type=USP-S",
            "min_price": 19.25,
            "max_price": 175,
            "mean_price": 41.53,
            "median_price": 36.55,
            "quantity": 61,
            "created_at": 1632899292,
            "updated_at": 1693686845,
        },
        {
            "market_hash_name": "USP-S | Whiteout (Minimal Wear)",
            "currency": "EUR",
            "suggested_price": 32.96,
            "item_page": "https://skinport.com/item/usp-s-whiteout-minimal-wear",
            "market_page": "https://skinport.com/market?item=Whiteout&cat=Pistol&type=USP-S",
            "min_price": 39.64,
            "max_price": 185,
            "mean_price": 77.81,
            "median_price": 69.99,
            "quantity": 61,
            "created_at": 1632921139,
            "updated_at": 1693686845,
        },
        {
            "market_hash_name": "USP-S | Whiteout (Well-Worn)",
            "currency": "EUR",
            "suggested_price": 17.62,
            "item_page": "https://skinport.com/item/usp-s-whiteout-well-worn",
            "market_page": "https://skinport.com/market?item=Whiteout&cat=Pistol&type=USP-S",
            "min_price": 19.79,
            "max_price": 32,
            "mean_price": 25.9,
            "median_price": 25.9,
            "quantity": 2,
            "created_at": 1632976633,
            "updated_at": 1693686845,
        },
        {
            "market_hash_name": "Valeria Phoenix Pin",
            "currency": "EUR",
            "suggested_price": 51.74,
            "item_page": "https://skinport.com/item/valeria-phoenix-pin",
            "market_page": "https://skinport.com/market?item=Valeria%20Phoenix%20Pin&cat=Collectible",
            "min_price": 53.95,
            "max_price": 80,
            "mean_price": 63.66,
            "median_price": 59.99,
            "quantity": 14,
            "created_at": 1535988295,
            "updated_at": 1693686845,
        },
    ]
    
    market_hash_name = "Valeria Phoenix Pin"
    min_price = 50
    
    out = [
        dct
        for dct in r
        if dct["market_hash_name"] == market_hash_name and dct["min_price"] >= min_price
    ]
    print(out)
    

    打印:

    [
        {
            "market_hash_name": "Valeria Phoenix Pin",
            "currency": "EUR",
            "suggested_price": 51.74,
            "item_page": "https://skinport.com/item/valeria-phoenix-pin",
            "market_page": "https://skinport.com/market?item=Valeria%20Phoenix%20Pin&cat=Collectible",
            "min_price": 53.95,
            "max_price": 80,
            "mean_price": 63.66,
            "median_price": 59.99,
            "quantity": 14,
            "created_at": 1535988295,
            "updated_at": 1693686845,
        }
    ]