JSON Python BS4从JSON提取中获取多个值

2024-07-04 06:03:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从j1info中提取“inventorylevel”

其输出为:

import requests
import json
import time
from datetime import datetime
from discord import Webhook, RequestsWebhookAdapter
from discord_webhook import DiscordWebhook, DiscordEmbed
import discord
from bs4 import BeautifulSoup
from discord.ext import commands

firsttime = True

while True:
    # How long to wait between checking (Its in seconds so 30 would be 30 seconds)
    if(firsttime == False):
        print("[ + ] Waiting")
        time.sleep(100000)
    firsttime = False


    print("[ + ] Checking Footlocker")
    # Accesses the first JSON item
    sizeurl = ("https://www.footlocker.co.nz/INTERSHOP/web/WFS/FootlockerAustraliaPacific-Footlocker_NZ-Site/en_GB/-/NZD/ViewProduct-ProductVariationSelect?BaseSKU=284101218804&InventoryServerity=ProductDetail")
    ref = ("https://www.footlocker.co.nz/INTERSHOP/web/WFS/FootlockerAustraliaPacific-Footlocker_NZ-Site/en_GB/-/NZD/ViewProduct-Start?v=284101218804")
    jsonurl = requests.get(sizeurl, headers={'referer': ref})
    data = jsonurl.content
    jsonone = json.loads(data)
    jsoninfo = jsonone['content']

    # Accesses the second JSON item
    soup = BeautifulSoup(jsoninfo, 'lxml')
    jsondiv = soup.find("div", { "data-ajaxcontent" : "product-variation-284101218804" }).get('data-product-variation-info-json')
    datatwo = '{"content":['+jsondiv+']}'
    jsontwo = json.loads(datatwo)
    for item in jsontwo['content']:
        inv = item['284101218804070']['inventoryLevel']
        print(inv)
[{'284101218804070': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '7', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'YELLOW'}, '284101218804080': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '8', 'quantityOptions': [1.0, 2.0],
'inventoryLevel': 'YELLOW'}, '284101218804085': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '8.5', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'YELLOW'}, '284101218804090': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '9', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'GREEN'}, '284101218804095': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '9.5', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'GREEN'}, '284101218804100': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '10', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'GREEN'}, '284101218804105': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '10.5', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'GREEN'}, '284101218804110': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '11', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'GREEN'}, '284101218804115': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '11.5', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'YELLOW'},
'284101218804120': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '12', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'GREEN'}, '284101218804130': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '13', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'GREEN'}, '284101218804140': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '14', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'YELLOW'}, '284101218804150': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '15', 'quantityOptions': [], 'inventoryLevel': 'RED'}}]
i have tried to use this piece of code: for item in jsontwo['content']: inv = item['284101218804070']['inventoryLevel'] print(inv)

但它只打印出一个库存级别,而我想打印出所有库存级别。我需要做什么


Tags: fromimportjsondatagreencontentitemprint
2条回答

jsontwo['content']的值是一个列表,其中包含以284101218804070等数字为键的对象,以及包含inventoryLevel的dictionary值

要打印所有inventoryLevel,您可以尝试:

for item in jsontwo['content']:
    for k, v in item.items():
        print(v['inventoryLevel'])

for item in jsontwo['content']:
    for v in item.values():
        print(v['inventoryLevel'])
data = [{'284101218804070': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '7', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'YELLOW'},
         '284101218804080': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '8', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'YELLOW'},
         '284101218804085': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '8.5', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'YELLOW'},
         '284101218804090': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '9', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'GREEN'},
         '284101218804095': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '9.5', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'GREEN'},
         '284101218804100': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '10', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'GREEN'},
         '284101218804105': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '10.5', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'GREEN'},
         '284101218804110': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '11', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'GREEN'},
         '284101218804115': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '11.5', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'YELLOW'},
         '284101218804120': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '12', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'GREEN'},
         '284101218804130': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '13', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'GREEN'},
         '284101218804140': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '14', 'quantityOptions': [1.0, 2.0], 'inventoryLevel': 'YELLOW'},
         '284101218804150': {'quantityMessage': '', 'quantityWarning': '', 'sizeValue': '15', 'quantityOptions': [], 'inventoryLevel': 'RED'}}]

for item in data:
    for key, value in item.items():
        print(f"{key}: {value.get('inventoryLevel')}")

输出:

284101218804070: YELLOW
284101218804080: YELLOW
284101218804085: YELLOW
284101218804090: GREEN
284101218804095: GREEN
284101218804100: GREEN
284101218804105: GREEN
284101218804110: GREEN
284101218804115: YELLOW
284101218804120: GREEN
284101218804130: GREEN
284101218804140: YELLOW
284101218804150: RED

相关问题 更多 >

    热门问题