抓取相似/相同名称的 json 字段

问题描述 投票:0回答:1

enter image description here我有两个字段命名相同(ish)。当我抓取时,我不断返回 amountusdcents 的值,但我只需要“金额”的值。

我在 json 查看器中添加了文本图像,因为它更容易。

[{"sizeOption":{"presentation":"3.5","value":3.5},"shoeCondition":"new_no_defects","boxCondition":"good_condition","lowestPriceCents":{"currency":"GBP ","amount":25764,"amountUsdCents":31294},"instantShipLowestPriceCents":{"currency":"GBP","amount":34686,"amountUsdCents":42051},"lastSoldPriceCents":{"currency": "GBP","amount":18200,"amountUsdCents":22000},"stockStatus":"single_in_stock"},{"sizeOption":

        for entry in output:
            if (entry['shoeCondition'] == "new_no_defects") and (entry['boxCondition'] == "good_condition"):
                size = entry['sizeOption']['presentation']
                try:
                    price_cents = entry['lastSoldPriceCents']['amount']
                except KeyError:
                    price_cents = 'OOS!'
                results.append((size, price_cents))

您可以忽略第一部分,只关注 price_cents = ...

预期结果 = 18200 ,我得到的结果 = 22000

不是抓取正确的值。有什么想法吗?

python json
1个回答
0
投票

你的预期结果(

25764
)是
entry.lowestPriceCents.amount
的值,所以你只要改变:

price_cents = entry['lastSoldPriceCents']['amount']

至:

price_cents = entry['lowestPriceCents']['amount']
© www.soinside.com 2019 - 2024. All rights reserved.