是否有任何函数可以从脚本标记获取所有大小

2024-09-20 06:42:03 发布

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

大小存在于脚本标记中,例如,"label":"10","price"重复使用不同的大小,如何在xpath中获得所有大小

<script type="text/javascript">
    var spConfig = new Product.Config({"attributes":{"149":{"id":"149","code":"size","label":"Size","options":[{"id":"12","label":"10","price":"0","oldPrice":"0","products":["527"]},{"id":"11","label":"12","price":"0","oldPrice":"0","products":["528"]},{"id":"10","label":"14","price":"0","oldPrice":"0","products":["529"]},{"id":"14","label":"6","price":"0","oldPrice":"0","products":["525"]},{"id":"13","label":"8","price":"0","oldPrice":"0","products":["526"]}]}},"template":"$#{price}","basePrice":"621","oldPrice":"621","productId":"524","chooseText":"Choose an Option...","taxConfig":{"includeTax":true,"showIncludeTax":true,"showBothPrices":false,"defaultTax":10,"currentTax":10,"inclTaxTitle":"Incl. Tax"}});
</script>

Tags: text标记脚本idtruevartypescript
1条回答
网友
1楼 · 发布于 2024-09-20 06:42:03

您看到的是Javascript脚本,不能用XPath(XPath是从XML文档中选择节点的语言)或CSS选择器解析Javascript。你知道吗

但是,您可以尝试手动解析数据。此示例使用re模块和json模块提取数据:

data = '''<script type="text/javascript">
        var spConfig = new Product.Config({"attributes":{"149":{"id":"149","code":"size","label":"Size","options":[{"id":"12","label":"10","price":"0","oldPrice":"0","products":["527"]},{"id":"11","label":"12","price":"0","oldPrice":"0","products":["528"]},{"id":"10","label":"14","price":"0","oldPrice":"0","products":["529"]},{"id":"14","label":"6","price":"0","oldPrice":"0","products":["525"]},{"id":"13","label":"8","price":"0","oldPrice":"0","products":["526"]}]}},"template":"$#{price}","basePrice":"621","oldPrice":"621","productId":"524","chooseText":"Choose an Option...","taxConfig":{"includeTax":true,"showIncludeTax":true,"showBothPrices":false,"defaultTax":10,"currentTax":10,"inclTaxTitle":"Incl. Tax"}});
    </script>'''

import re
import json

json_data = json.loads( re.findall(r'Config\((.*?)\);', data)[0] )

print(json.dumps(json_data, indent=4))

印刷品:

{
    "attributes": {
        "149": {
            "id": "149",
            "code": "size",
            "label": "Size",
            "options": [
                {
                    "id": "12",
                    "label": "10",
                    "price": "0",
                    "oldPrice": "0",
                    "products": [
                        "527"
                    ]
                },
                {
                    "id": "11",
                    "label": "12",
                    "price": "0",
                    "oldPrice": "0",
                    "products": [
                        "528"
                    ]
                },
                {
                    "id": "10",
                    "label": "14",
                    "price": "0",
                    "oldPrice": "0",
                    "products": [
                        "529"
                    ]
                },
                {
                    "id": "14",
                    "label": "6",
                    "price": "0",
                    "oldPrice": "0",
                    "products": [
                        "525"
                    ]
                },
                {
                    "id": "13",
                    "label": "8",
                    "price": "0",
                    "oldPrice": "0",
                    "products": [
                        "526"
                    ]
                }
            ]
        }
    },
    "template": "$#{price}",
    "basePrice": "621",
    "oldPrice": "621",
    "productId": "524",
    "chooseText": "Choose an Option...",
    "taxConfig": {
        "includeTax": true,
        "showIncludeTax": true,
        "showBothPrices": false,
        "defaultTax": 10,
        "currentTax": 10,
        "inclTaxTitle": "Incl. Tax"
    }
}

要获取一些数据,请使用变量json_data作为dict:

print(json_data['basePrice'])

将打印:

621

相关问题 更多 >