在提取JSON值时获取错误“KeyError”

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

我成功地从JSON中提取响应。但是,我无法列出所有或提取我需要的密钥及其对

以下是我的代码:

import requests

response = requests.get("https://www.woolworths.com.au/apis/ui/Product/Specials/half-price?GroupID=948&isMobile=false&pageNumber=1&pageSize=36&richRelevanceId=SP_948&sortType=Personalised")

data = response.json()

我试着做data['Stockcode']

但没有运气或我使用data['Product']

它说:

>>> data['Product']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'Product'
>>> data['Products']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'Products'
python json keyvaluepair
1个回答
0
投票

尝试:

>>> data['Items'][0]['Products']

打印数据并查看其数据结构如何构建它然后您可以根据需要提取值

© www.soinside.com 2019 - 2024. All rights reserved.