JSON Python BS4-从JSON提取中获取多个值
我试图从j1info中提取“inventorylevel” 其输出为:JSON Python BS4-从JSON提取中获取多个值,python,json,beautifulsoup,Python,Json,Beautifulsoup,我试图从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 Beau
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)
但它只打印出一个库存级别,而我想打印出所有库存级别。我需要做什么
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:黄色
28410121880480:黄色
284101218804805:黄色
284101218804090:绿色
284101218804095:绿色
284101218804100:绿色
284101218804105:绿色
284101218804110:绿色
284101218804115:黄色
284101218804120:绿色
284101218804130:绿色
284101218804140:黄色
284101218804150:红色
jsontwo['content']的值是一个列表,其中包含以284101218804070
等数字为键的对象,以及包含inventoryLevel
的字典值
要打印所有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'])