0

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()
5
  • 1
    items.append({'Graphics Card": {"Brands": brand, "Products": product_name, "Shipping": shipping }}) ??? Commented Feb 27, 2017 at 18:45
  • 1
    What output are you getting right now? Commented Feb 27, 2017 at 18:53
  • You know your script is illegal, right? You specifically agree not to access or attempt to access the Website, or any portion thereof, through any automated means, including but not limited to the use of scripts or web crawlers - kb.newegg.com/Article/Index/12/3?id=1165 Commented Feb 27, 2017 at 19:00
  • @Chirag only one level deep [{ "xxx": "xxx" }] Commented Feb 27, 2017 at 19:04
  • @cricket_007 No I didn't, but per the bottom line " You agree that you are solely responsible for any breach of your obligations under this Policy & Agreement and for the consequences (including any loss or damage which Newegg.com may suffer) of any such breach." Again, this is just some practice code I found on YouTube. I don't have any desire to use it only learn. Commented Feb 27, 2017 at 19:11

1 Answer 1

1

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": [ ... ]
    ]
}
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect! I've been staring at this code all morning trying to figure it out. I was worried about "performance" but I'm still new to jSon that I was trying to get it running. Thank you again, I really appreciate it!
All the letters are capitalized, by the way. JSON.... JavaScript Object Notation is an acronym... Anyways, Python dictionaries mostly map directly into JSON objects, and lists to arrays, so just think about python objects first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.