我有这个对象:
"products":[
{
"title":"ASUS ZenFone 4 Max",
"author":"1",
"date":"2017-12-17 21:49:30",
"date_gmt":"0000-00-00 00:00:00",
"content":"",
"status":"draft",
"comment_status":"open",
"image":"",
"gallery":[
],
"meta":{
"post_views_count":"0",
"aps-product-price":"0",
"aps-product-sku":"SKU",
"aps-product-stock":"InStock",
"aps-product-qty":"0",
"aps-product-on-sale":"no",
"aps-product-sale-price":"0",
"aps-product-sale-start":"",
"aps-product-sale-end":"",
"aps-product-features":[
{
"name":"CPU",
"icon":"cpu",
"value":"CPU Feature"
},
{
"name":"RAM",
"icon":"ram",
"value":"RAM Feature"
},
{
"name":"Storage",
"icon":"hdd",
"value":"Storage Feature"
},
{
"name":"Display",
"icon":"display",
"value":"Display Feature"
},
{
"name":"Camera",
"icon":"camera",
"value":"Camera Feature"
},
{
"name":"OS",
"icon":"cog",
"value":"OS Feature"
}
],
"aps-product-videos":[
],
"aps-attr-group-684":{
"618":"General-Model-Attribute",
"556":"01-12-2017",
"644":"02-12-2017",
"659":"Available"
},
"aps-attr-group-685":{
"578":"Design-Dimensions-Attribute",
"678":"Design-Weight-Attribute",
"572":"*Design-Colors-Attribute\r\n*Design-Colors-Attribute\r\n*Design-Colors-Attribute"
},
"aps-attr-group-687":{
"655":"Display-Dimensions-Attribute",
"861":"Display-Technology-Attribute",
"581":"Display-Protection-Attribute",
"645":"Display-Resolution-Attribute"
},
"aps-attr-group-690":{
"623":"Software-Operating System-Attribute",
"668":"Software-User Interface-Attribute"
},
"aps-attr-group-689":{
"631":"Camera-Front-Attribute",
"590":"Camera-Front-Camera-Features-Attribute",
"647":"Camera-Rear-Camera-Attribute",
"863":"Camera-Rear-Camera-Features-Attribute"
},
"aps-attr-group-691":{
"571":"Hardware-Chipset-Attribute",
"573":"Hardware-CPU-Attribute",
"596":"Hardware-GPU-Attribute",
"639":"512 MB",
"612":"Hardware-InternalStorage-Attribute",
"570":"Hardware-CardSlot-Attribute",
"651":"Hardware-Sensors-Attribute"
},
"aps-attr-group-686":{
"553":"Network-Networks-Attribute",
"654":"Network-SIM-Attribute"
},
"aps-attr-group-692":{
"564":"Connectivity-Bluetooth-Attribute",
"679":"Connectivity-WiFi-Attribute",
"680":"No",
"666":"Connectivity-USB-Attribute",
"595":"Connectivity-GPS-Attribute",
"621":"No"
},
"aps-attr-group-688":{
"558":"Media-AudioPlayback-Attribute",
"672":"Media-Video-Playback-Attribute",
"671":"Media-Video-Out-Attribute",
"593":"Media-FMRADIO-Attribute",
"555":"Media-Alert-Types-Attribute",
"646":"Media-Ring-Tones-Attribute",
"615":"Media-Loudspeaker-Attribute",
"598":"Media-Handsfree-Attribute"
},
"aps-attr-group-695":{
"569":"Battery-Capacity-Attribute",
"629":"Removable",
"862":"Battery-Charging-Attribute",
"658":"Battery-Standby-Attribute",
"662":"Battery-TalkTime-Attribute",
"620":"Battery-MusicPlay-Attribute"
},
"aps-product-offers":[
],
"aps-product-rating":{
"design":5,
"display":6,
"camera":5,
"performance":5,
"battery":6
},
"aps-product-rating-total":"5.4",
"aps-custom-tabs":[
],
"_disable_fbc":"",
"_yoast_wpseo_primary_aps-cats":"",
"_yoast_wpseo_content_score":"30"
},
"terms":{
"aps-cats":[
"smartphones"
]
}
},...]
我需要在循环中动态实现这一点。例如:
#Initial declaration
products = {"products":{}}
#Add items in the products object
products["title"] = "ASUS ZenFone 4 Max"
products["author"] = "1"
for items in product_list:
products["meta"]["post_views_count"] = 0
products["meta"]["aps-product-sku"] = "SKU"
我似乎无法弄清楚如何添加下一个也有“title”,“author”,“meta”的产品(这也是一个具有键值对的对象)等。
我希望你能理解我的疑问。我正在进行网络抓取:)
谢谢
答案 0 :(得分:0)
试试这样:
products = []
product1 = {}
product1["title"] = "ASUS ZenFone 4 Max"
product2 = {}
product2["title"] = "Galaxy S8"
products.append(product1)
products.append(product2)
print(products)
答案 1 :(得分:0)
if __name__ == '__main__':
new_list = list()
new_product_dict = {
"title":"new title",
"author":"new author",
"date":"2017-12-18 21:49:30",
"date_gmt":"0000-00-00 00:00:00",
"content":"",
"status":"draft",
"comment_status":"open",
"image":""}
for items in product_list.values():
new_list = [new_product_dict]
items.append(new_list)
print(items)
这种方法对我有用,当然你可以在for循环中添加更多的dicts