更改API中的嵌套URL值

2024-09-27 07:31:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始玩API,我有一个产品列表,我想从客户网站上获取。一切正常,但我意识到,如果我直接将它们上传到数据库,它们返回URL的方式将不起作用,因为它们前面有一个“/”,浏览器会自动假定它是一个文件,而不是https://。我正在尝试遍历返回的值并搜索所有URL,然后删除//或将https:添加到它的前面。请注意,所有URL都显示在JSON中嵌套的列表中

目前,我已经编写了可以循环使用当前JSON的代码,但是JSON的结构并不总是相同的,并且键会根据产品的类别甚至产品而变化。我希望能够遍历JSON,确定它是否是一个列表,遍历它,并更改URL和SRC键。除非另有办法

"category": "Thermostats",
"description": {
    "long": "The ultimate controller for precise comfort",
    "short": "Ultra Smart Thermostat"
},
"disclaimers": [
    {
        "body": "S30 is compatible with Lennox HVAC products and requires separate purchase of Amazon Echo or Echo Dot. At this time, Amazon asserts that Amazon Echo devices are not available for purchase in all countries. Therefore, this reference is intended for use with US-based thermostats only. Amazon, Alexa and all related logos are trademarks of Amazon.com, Inc. or its affiliates.",
        "id": 54
    },
    {
        "body": "Apple HomeKit technology provides an easy, secure way to control your home\u2019s lights, doors, thermostats, and more from your iPhone, iPad, or Apple Watch. To control this HomeKit-enabled accessory, iOS 10.2.1 or later is recommended. Apple, Apple Watch, iPad, iPad Air, iPhone, and iPod touch are trademarks of Apple Inc., registered in the U.S. and other countries. HomeKit is a trademark of Apple Inc.",
        "id": 56
    }
],
"efficiencies": null,
"energyStarQualified": false,
"energyStarSouthQualified": false,
"formattedModelNumber": "iComfort S30",
"id": 190,
"image": {
    "default": {
        "alt": "iComfort S30\u00ae Ultra Smart Thermostat",
        "src": "//resources.lennox.com/fileuploads/e5cef8c9-67f6-479e-83f2-f56d2e067850cgi63393_main_default.png"
    },
    "lg": {
        "alt": "iComfort S30\u00ae Ultra Smart Thermostat",
        "src": "//resources.lennox.com/fileuploads/682ef372-b8cd-4866-a1f0-088482d57b29cgi63393_main_lg.png"
    },
    "md": {
        "alt": "iComfort S30\u00ae Ultra Smart Thermostat",
        "src": "//resources.lennox.com/fileuploads/29c45676-ba25-4743-ba23-ac1042359136cgi63393_main_md.png"
    },
    "sm": null,
    "x-lg": null,
    "x-sm": null
},
"isLimitedAvailability": false,
"limitedAvailabilityContent": null,
"limitedAvailabilityLinkText": null,
"literature": [
    {
        "title": "iComfort Smart Thermostats Product Brochure",
        "type": "Product Brochure",
        "url": "//resources.lennox.com/fileuploads/a196ddc3-0959-4433-9f37-b07147d3d418LennoxiComfortThermostats.pdf"
    },
    {
        "title": "iComfort S30 Product Brochure",
        "type": "Product Brochure",
        "url": "//resources.lennox.com/fileuploads/e0c68728-91f3-4de7-8872-065c850aa9faiComfortS30_Product_Brochure.pdf"
    },
    {
        "title": "iComfort S30 Homeowner Manual",
        "type": "Homeowners Manual",
        "url": "//resources.lennox.com/fileuploads/706b3305-e698-4f44-bc3c-764c144d74beiComfort%20_S30_%20Homeowner_Manual.pdf"
    },
    {
        "title": "iComfort S30 Installation Manual and Setup Guide",
        "type": "Installation Manual",
        "url": "//resources.lennox.com/fileuploads/b9863d33-f22e-4fc2-ae10-0a143901528bLennox_iComfortS30_Installation_Manual_Setup_Guide.pdf"
    },
    {
        "title": "Ultimate Comfort System Products Brochure",
        "type": "System Literature",
        "url": "//resources.lennox.com/fileuploads/dc0c5077-c8ea-4933-87f3-1facef93f38b10D29_UCS_84800_0516.pdf"
    }
],
"modelName": "iComfort\u00ae S30 Ultra Smart Thermostat",
"modelNumber": "iComfort S30",
"priceGuide": "$$$$",
"series": "None",
"solarReady": false,
"supportImage": {
    "default": {
        "alt": "living room",
        "src": "//resources.lennox.com/fileuploads/b8ea938d-c4c4-4838-9e64-acd137b30ce3s30-promo-default.jpg"
    },
    "lg": {
        "alt": "living room",
        "src": "//resources.lennox.com/fileuploads/1ff69e07-90bf-4f73-8bb5-8288e5c417f1s30-promo-lg.jpg"
    },
    "md": {
        "alt": "living room",
        "src": "//resources.lennox.com/fileuploads/64d481eb-80a1-469d-b473-b2d2d505238es30-promo-md.jpg"
    },
    "sm": null,
    "x-lg": null,
    "x-sm": null
},
"taxCreditQualifying": false,
"warrantyBlurb": "5-Year Limited Warranty",
"warrantyEligible": false
}

Tags: andsrccomfalseapplesmartaltnull
1条回答
网友
1楼 · 发布于 2024-09-27 07:31:50

您可以将整个内容设置为str并替换为:

import json

json.loads(json.dumps(res).replace('//', ''))

部分输出:

...
'image': {'default': {'alt': 'iComfort S30® Ultra Smart Thermostat',
   'src': 'resources.lennox.com/fileuploads/e5cef8c9-67f6-479e-83f2-f56d2e067850cgi63393_main_default.png'},
  'lg': {'alt': 'iComfort S30® Ultra Smart Thermostat',
   'src': 'resources.lennox.com/fileuploads/682ef372-b8cd-4866-a1f0-088482d57b29cgi63393_main_lg.png'},
  'md': {'alt': 'iComfort S30® Ultra Smart Thermostat',
   'src': 'resources.lennox.com/fileuploads/29c45676-ba25-4743-ba23-ac1042359136cgi63393_main_md.png'},
...

相关问题 更多 >

    热门问题