Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python嵌套jSon对象_Python_Json - Fatal编程技术网

Python嵌套jSon对象

Python嵌套jSon对象,python,json,Python,Json,我试图将我的对象嵌套到另一个名为“图形卡”的对象中,但我很难找到它。我尝试了一些方法,但没有得到我想要的结果 [ { "Graphics Card": { "Brands": "Brand Name", "Products": "Product Name", "Shipping": "Brand Name" } } ] 下面是我的代码。感谢您的帮助。谢谢大家! from urllib.request i

我试图将我的对象嵌套到另一个名为“图形卡”的对象中,但我很难找到它。我尝试了一些方法,但没有得到我想要的结果

[
  {
    "Graphics Card":
      {
        "Brands": "Brand Name",  
        "Products": "Product Name",
        "Shipping": "Brand Name"
      }
  }
]
下面是我的代码。感谢您的帮助。谢谢大家!

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()

你问题中的JSON没有真正意义

人们会预料到

{“图形卡”:[{object1},{object2},…]}

或者可能是这样,但您会丢失数据中的关联值。。。所以可能不是

{“图形卡”:{“品牌”:[…],“产品”:[…],“运输”:[…]}

也就是说,你想这么做

final_items = { "Graphics Cards": items }
print(json.dumps(final_items, sort_keys=True, indent=4))
你的代码运行良好

{
    "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"
        },
不过,对于“更好”的JSON数据,建议是:将每个“品牌”分组

{ 
    "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": [ ... ]
    ]
}

items.append({'Graphics Card:{“Brands”:brand,“Products”:product_name,“Shipping”:Shipping})
???您现在得到了什么输出?您知道您的脚本是非法的,对吗?您明确同意不通过任何自动方式访问或试图访问网站或其任何部分,包括但不限于使用脚本或网络爬虫-@Chirag仅一级深度[{“xxx”:“xxx”}]@cricket_007不,我没有,但根据底线“您同意您对违反本政策和协议项下的义务以及任何此类违反行为的后果(包括Newegg.com可能遭受的任何损失或损害)承担全部责任。“再说一遍,这只是我在YouTube上找到的一些练习代码。我不想使用它,只想学习。太好了!我整个上午都在盯着这些代码想弄明白。我担心的是“性能”但我对jSon还是很陌生,我一直在尝试让它运行。再次感谢你,我真的很感激!顺便说一句,所有字母都是大写的。jSon…JavaScript对象表示法是一个缩写词…总之,Python字典大多直接映射到jSon对象,列表到数组,所以只需首先考虑Python对象。