使用json库/请求解析JSON文件

时间:2019-01-30 16:30:24

标签: python json python-3.x python-requests

我正在尝试解析JSON文件,这给我带来了一些麻烦。我之前解析过JSON,但从未遇到过此问题。

该站点受cloudflare保护,我使用python模块绕过了它。返回的内容在json中。看起来像这样:

stock{

{'sizes': 
   {
   'B79759_550': {'size': '5', 'stock': 68}, 
   'B79759_590': {'size': '7', 'stock': 20}, 
   'B79759_600': {'size': '7.5', 'stock': 5}, 
   'B79759_610': {'size': '8', 'stock': 681}, 
   'B79759_620': {'size': '8.5', 'stock': 807}, 
   'B79759_630': {'size': '9', 'stock': 775}, 
   'B79759_640': {'size': '9.5', 'stock': 978}, 
   'B79759_650': {'size': '10', 'stock': 1097}, 
   'B79759_660': {'size': '10.5', 'stock': 814}, 
   'B79759_670': {'size': '11', 'stock': 779}, 
   'B79759_680': {'size': '11.5', 'stock': 581}, 
   'B79759_690': {'size': '12', 'stock': 643}, 
   'B79759_700': {'size': '12.5', 'stock': 5}, 
   'B79759_710': {'size': '13', 'stock': 620}, 
   'B79759_730': {'size': '14', 'stock': 19}
   },
   'total_stock': 7892
}
}

到目前为止,我已经成功使用以下代码进入了问题(以B79759开头的数字):

resp = scraper.post(link,data=payload,headers=headers).json()
stock = resp["stock"]
sizes = stock["sizes"]
for size in sizes:
    print(size)

现在,这仅给了我SKU(“ B79759_550,B79759_590,B79759_600等) 我也希望能够获得尺寸和库存数量。我尝试这样做:

resp = scraper.post(link,data=payload,headers=headers).json()
stock = resp["stock"]
sizes = stock["size"]
stock = sizes["stock"]
actual_size = sizes["size"]
print(stock)

这两个都导致了[“ size”]的关键错误。我以前从未遇到过这个问题,也不知道我到底在做什么错。

2 个答案:

答案 0 :(得分:1)

resp = scraper.post(link,data=payload,headers=headers).json()
stock = resp["stock"]
sizes = stock["sizes"]
for sku in sizes:
    print(sku)
    print(sizes[sku]["size"])

您正在打印的第一个代码段越来越近了,它的大小(我将其更改为sku以便更轻松地解析头部中的JSON)正在打印的是每个关键字,您需要找到每个对象并获取大小。

答案 1 :(得分:0)

格式化使用刮板将获得的JSON响应:

response = {
    "stock": {
        "sizes": {
            "B79759_550": {
                "size": "5",
                "stock": 68
            },
            "B79759_590": {
                "size": "7",
                "stock": 20
            },
            "B79759_600": {
                "size": "7.5",
                "stock": 5
            },
            "B79759_610": {
                "size": "8",
                "stock": 681
            },
            "B79759_620": {
                "size": "8.5",
                "stock": 807
            },
            "B79759_630": {
                "size": "9",
                "stock": 775
            },
            "B79759_640": {
                "size": "9.5",
                "stock": 978
            },
            "B79759_650": {
                "size": "10",
                "stock": 1097
            },
            "B79759_660": {
                "size": "10.5",
                "stock": 814
            },
            "B79759_670": {
                "size": "11",
                "stock": 779
            },
            "B79759_680": {
                "size": "11.5",
                "stock": 581
            },
            "B79759_690": {
                "size": "12",
                "stock": 643
            },
            "B79759_700": {
                "size": "12.5",
                "stock": 5
            },
            "B79759_710": {
                "size": "13",
                "stock": 620
            },
            "B79759_730": {
                "size": "14",
                "stock": 19
            }
        },
        "total_stock": 7892
    }
}

以下代码行将使用list comprehensions返回所有大小的列表:

stock_details = response['stock']
stock_setails = stock_details['sizes'].values()
size = [size['size'] for size in stock_setails]
print(size)