简体   繁体   English

访问内的javascript值<script> tag with xpath

[英]access javascript value within the <script> tag with xpath

from this tag: 从这个标签:

<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>

I need to access some specific values of different functions (products, products.brand, impressions and impressions.id). 我需要访问不同功能的某些特定值(产品,products.brand,印象和impressions.id)。

I tried to convert it to json dictionary, but it is a list and it gets converted into an array and I cannot access it with a "name" value. 我试图将其转换为json字典,但它是一个列表,并且被转换为数组,因此无法使用“名称”值进行访问。

How can I do this? 我怎样才能做到这一点?

An example would be to select the script using some unique keyword which is only present in this script. 一个示例是使用仅在该脚本中存在的某些唯一关键字来选择脚本。 Once you have that, you can use re_first to get the desired value. 一旦有了,就可以使用re_first获得所需的值。 Take this as an example: 以此为例:

        product_css = "script:contains('productPage')::text"
        regex = 'name": "(.+?)"'
    product_name = response.css(product_count_css).re_first(regex)

This will extract the first name. 这将提取名字。 You can use re to tweak it further. 您可以使用re进行进一步调整。 Best of luck. 祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM