Reputation: 429
I'm trying to nest my objects into another object called "Graphics Card" but I'm having trouble figuring it out. I've tried a few thing but I'm not getting the output that I'm looking for.
[
{
"Graphics Card":
{
"Brands": "Brand Name",
"Products": "Product Name",
"Shipping": "Brand Name"
}
}
]
Below is my code. Any help is appreciated. Thank you!
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import json
my_url = 'https://www.newegg.com/Video-Cards-Video-Devices/Category/ID-38?Tpk=graphics%20cards'
# opening up connection, grabbing the page
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
# html parsing
page_soup = soup(page_html, "html.parser")
# grabs each product
containers = page_soup.findAll("div", {"class":"item-container"})
items = []
for container in containers:
brand = container.div.div.a.img["title"]
title_container = container.findAll("a",{"class":"item-title"})
product_name = title_container[0].text
shipping_container = container.findAll("li", {"class":"price-ship"})
shipping = shipping_container[0].text.strip()
items.append({"Brands": brand, "Products": product_name, "Shipping": shipping })
print(json.dumps(items, sort_keys=True, indent=4))
fout = open("text.json", 'w')
json.dump(items, fout, sort_keys=True, indent=4)
fout.close()
Upvotes: 0
Views: 541
Reputation: 191691
The JSON in your question doesn't really make sense.
One would expect
{ "graphics cards": [ {object1}, {object2},... ] }
Or maybe this, but that you lose the associated values in the data... so probably not
{ "graphics cards": { "brands": [ ... ], "products": [...], "shipping": [...] }
That being said, you want to do this.
final_items = { "Graphics Cards": items }
print(json.dumps(final_items, sort_keys=True, indent=4))
And your code works fine.
{
"Graphics Cards": [
{
"Brands": "GIGABYTE",
"Products": "GIGABYTE GeForce GTX 1060 Windforce OC GV-N1060WF2OC-6GD Video Card",
"Shipping": "Free Shipping"
},
{
"Brands": "XFX",
"Products": "XFX Radeon GTR RX 480 DirectX 12 RX-480P8DBA6 Black Edition Video Card",
"Shipping": "$4.99 Shipping"
},
Suggestion, though for "better" JSON data: group each "brand" together.
{
"cards": [
"GIGABYTE": [
{
"Products": "GIGABYTE GeForce GTX 1060 Windforce OC GV-N1060WF2OC-6GD Video Card",
"Shipping": "Free Shipping"
},
{
"Products": "GIGABYTE GeForce GTX 1050 Ti DirectX 12 GV-N105TWF2OC-4GD Video Card",
"Shipping": "Free Shipping"
}
],
"XFX": [ ... ]
]
}
Upvotes: 1