需要初学者的简单帮助:)下面的python代码:
url = "theurl"
headers = {'Accept': 'application/json', 'X-auth-Token': 'thetoken'}
response = requests.get(url, headers=headers)
json_data = response.json()
nexturl = json_data['next']以下是对请求的答复:
{
"data": [
{
"id": 112,
"name": "sample",
"type": "sample",
"sku": "SKUF46386",
"description": "",
"weight": 0,
"width": 0,
"depth": 0,
"height": 0,
"price": 162,
"cost_price": 0,
"retail_price": 0,
"sale_price": 0,
"map_price": 0,
"tax_class_id": 0,
"product_tax_code": "",
"calculated_price": 162,
"categories": [
25
]
],
"meta": {
"pagination": {
"total": 14526,
"count": 50,
"per_page": 50,
"current_page": 1,
"total_pages": 291,
"links": {
"next": "?page=2&limit=50",
"current": "?page=1&limit=50"
},
"too_many": false
}
}}
如何获取这个值: meta.pagination.links.next并将其存储在一个新变量中。当我尝试运行代码时,Idle附带了一条消息: KeyError:'next‘
发布于 2022-09-05 10:50:17
response是一本字典。要从字典中获取指定键的值,您需要:your_dict['key']。
在您的例子中,有一个response字典,它有两个键:"data"和"meta"。因此,要获得"meta"键的值(也是一个字典),您必须:“`response”meta。
您要查找的值(用于键"next")存储得更深--在您刚刚用"meta"键获得的字典中,有一个"pagination"键,它的值是另一个带有键"links"的字典,它的值又是另一个具有希望键"next"的字典。
因此,要获得"next"键的值,您必须:
response["meta"]["pagination"]["links"]["next"]`https://stackoverflow.com/questions/73608009
复制相似问题