[英]JSON Python BS4 - Getting more than one value out of JSON extraction
I am trying to extract " inventorylevel" from j1info.我正在尝试从 j1info 中提取“inventorylevel”。
The output of this is: output 是:
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)
But it only prints out one inventory level, when I want to print out all of them.但是当我想打印所有库存时,它只打印出一个库存级别。 What do I need to do?我需要做什么?
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')}")
Output: Output:
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
The value of jsontwo['content']
is a list that contains objects with the number like 284101218804070
as the key and a value of dictionary that includes the inventoryLevel
. jsontwo['content']
的值是一个列表,其中包含以284101218804070
等数字为键的对象和包含inventoryLevel
的字典值。
To print all the inventoryLevel's, you can try:要打印所有库存级别,您可以尝试:
for item in jsontwo['content']:
for k, v in item.items():
print(v['inventoryLevel'])
Or或者
for item in jsontwo['content']:
for v in item.values():
print(v['inventoryLevel'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.