2017-04-22 75 views
0

我成功地從JSON中提取響應。然而,我無法列出所有或提取什麼,我需要的關鍵及其對提取JSON值時獲取錯誤「KeyError」

下面是我的代碼:

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' 

回答

0

試:

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

打印數據,看看它如何然後構建了數據結構,你可以提取值根據自己的需要