从API更改嵌套的URL值

时间:2019-05-15 14:39:28

标签: python python-requests

我才刚刚开始使用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
}

1 个答案:

答案 0 :(得分:1)

您可以将整个内容制作为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'},
...