我有下面的代码,它发出一个API调用,返回关于市场上每个列表的信息.出于隐私考虑,我已经删除了客户ID和客户机密.

"""
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来实现这一点.

谢谢你的帮助!

推荐答案

您可以使用列表理解来过滤您的结果.在变量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,
    }
]

Python相关问答推荐

将列中的滚动值集转换为单元格中的单个值

从多行文本中提取事件对

尽管进程输出错误消息,subProcess.check_call的CalledProcess错误.stderr为无

inspect_asm不给出输出

收件箱转换错误- polars.exceptions. ComputeHelp- pandera(0.19.0b3)带有polars

使用matplotlib pcolormesh,如何停止从一行绘制的磁贴连接到上下行?

覆盖Django rest响应,仅返回PK

从DataFrame.apply创建DataFrame

Python plt.text中重叠,包adjust_text不起作用,如何修复?

剧作家Python:expect(locator).to_be_visible()vs locator.wait_for()

使用LineConnection动画1D数据

在内部列表上滚动窗口

Pandas 第二小值有条件

为什么默认情况下所有Python类都是可调用的?

如何将一个动态分配的C数组转换为Numpy数组,并在C扩展模块中返回给Python

Pandas:将多级列名改为一级

不允许访问非IPM文件夹

如何在两列上groupBy,并使用pyspark计算每个分组列的平均总价值

合并与拼接并举

基于另一列的GROUP-BY聚合将列添加到Polars LazyFrame