首页 > 解决方案 > 用正则表达式找到模式,然后将其转换以获得python dict对象

问题描述

我试图从这段代码的结果中提取一些键值对。

response.xpath('/html/body/script[contains(.,pageType)]').extract()

类型是“列表”,我需要将其转换为字典。我最初的方法是将其转换为字符串,但后来我发现我仍然无法使用 key["value"] 方法访问键值对。

有人建议我用正则表达式找到模式,用 json.load 转换它以获得 python dict 对象。但我似乎找不到用正则表达式找到模式的方法。

我应该如何用正则表达式隔离键值变量?(例如:键“价格”和值“34,95”,然后将它们存储到字典中?感谢您的帮助!

<script type = "text/javascript" > dataLayer.push({
    "pageType": "productPage", "ecommerce": {
        "currencyCode": "EUR",
        "detail": {
            "actionField": {"list": "Detail", "action": "detail"},
            "products": [{
                "name": "Desodorante Spray Alien",
                "id": "10483558",
                "price": "34,95",
                "brand": "MUGLER",
                "category": "higiene\/desodorantes",
                "variant": "100 ML",
                "dimension5": "Mucho stock",
                "dimension6": "Unisex",
                "dimension7": "CLARINS SPAIN, S.A",
                "dimension8": "No",
                "dimension9": "",
                "metric2": 1
            }]
        },
        "impressions": [{
            "name": "Angel men recarga Eau de Toilette",
            "id": "10059432",
            "price": "47.95",
            "brand": "MUGLER",
            "category": "perfumes_hombre_edt",
            "variant": "100 ML ",
            "list": "you may want",
            "position": 1
        }, {
            "name": "Angel men rubber flask Eau de Toilette",
            "id": "10351154",
            "price": "42.95",
            "brand": "MUGLER",
            "category": "perfumes_hombre_edt",
            "variant": "50 ML ",
            "list": "you may want",
            "position": 2
        }, {
            "name": "Alien Shower Milk",
            "id": "10483565",
            "price": "26.00",
            "brand": "MUGLER",
            "category": "higiene_geles",
            "variant": "200 ML ",
            "list": "you may want",
            "position": 3
        }, {
            "name": "Amen Desodorante en Stick",
            "id": "10532706",
            "price": "21.95",
            "brand": "MUGLER",
            "category": "hombre_desodorantes",
            "variant": "75 ML ",
            "list": "you may want",
            "position": 4
        }]
    }
});
(window["rrApiOnReady"] = window["rrApiOnReady"] || []).push(function () {
    retailrocket.productsGroup.post({
        "groupId": 10483558,
        "name": "Desodorante Spray Alien",
        "price": 34.95,
        "pictureUrl": "https://ima.douglas.es/img/1467/desodorante_spray_alien-0-.png",
        "url": "https://douglas.es/p/mugler/desodorante_spray_alien",
        "isAvailable": true,
        "categoryPaths": ["Higiene/Corporal", "Corporal", "Corporal/Higiene", "Higiene", "Higiene/Desodorante", "Marca/Mugler"],
        "description": "El elixir de feminidad y de sensualidad del Eau de Parfum Alien en su versión desodorante en spray. Déjate envolver con los mismos acordes de la fragancia.",
        "vendor": "MUGLER",
        "products": {
            "10483558": {
                "isAvailable": true,
                "name": "Desodorante Spray Alien",
                "size": "100",
                "url": "https://douglas.es/p/mugler/desodorante_spray_alien",
                "pictureUrl": "https://ima.douglas.es/img/1467/desodorante_spray_alien-0-.png",
                "price": 34.95,
                "oldPrice": 34.95,
                "params": {}
            }
        },
        "params": {"medida": "ML", "subTitle": "Todo tipo de piel"},
        "model": "Desodorante Spray Alien",
        "typePrefix": "higiene_desodorantes",
        "oldPrice": 34.95
    });
    rrApi.groupView([10483558]);
});
App.page.webshop = "DOU";
App.page.warehouse = ["ALM"];
App.page.codPostal = "";
</script>

标签: pythonregex

解决方案


您可以使用此正则表达式来捕获 JSON 字符串:

dataLayer\.push\((.+?)\);

然后使用Python解析内容\1

https://regex101.com/r/cVXxRA/1/


推荐阅读