1

I'm making a script using node.js and i need to parse a script inside a website, exactly i need 2 part not only the entire script. The 2 parts are "stock" and "local" and their values.

<script id="z-vegas-pdp-props" type="application/json">


![CDATA[{
            "layout": "cover",
            "model": {
                "layout": "cover",
                "priceInfo": {
                    "showPriceHint": false,
                    "priceHintType": ""
                },
            "mediaInfo": {
                "currentIndex": 0
            },
                    
            "price": {
                "currency": "EUR",
                "value": 129.99,
                "formatted": "129,99 €"
            },
            "originalPrice": {
                "currency": "EUR",
                "value": 129.99,
                "formatted": "129,99 €"
            },
            "available": false,
            "stock": 0
        }
        //here the scripts continues but i "trimmed it to make it more easy"
    </script>

This is what i made but it's parsing all the code and not only the parts that i need.

let root = HTMLParser.parse(response.body);
    let availabiltyDiv = root.querySelector("#z-vegas-pdp-props")
    console.log(availabiltyDiv)

1 Answer 1

1

The data you're looking for is hiding in json format inside the CDATA in the script. So you first need to extract the json string, parse it and get to the target data. Incidentally, the json string sample in your question is malformed, but is presumably well formed in the actual script. Also, there is no local in your sample, so I'll use another value instead.

All in all:

const jstring = availabiltyDiv.childNodes[0].nodeValue.split('CDATA[')[1].split(']')[0]
const target = JSON.parse(jstring);

#to get to (for example) price and stock:
const price = target.model.price.formatted;
const stock = target.model.stock;

console.log(price,stock)

Output:

"129,99 €" 0

EDIT:

As expalined in the respoinse to your comment, I don't have access to your actual availabilityDiv but you can also try replacing

const jstring = availabiltyDiv.childNodes[0].nodeValue.split('CDATA[')[1].split(']')[0]

with

const jstring = availabiltyDiv.innerHTML.split('CDATA[')[1].split(']')[0]

or with

const jstring = availabiltyDiv.innerText.split('CDATA[')[1].split(']')[0]

and see if they work.

Sign up to request clarification or add additional context in comments.

3 Comments

i get this error: Cannot read property 'split' of undefined at... how can i fix it?
@DennyVidotto As I said, the json in your question is malformed and I had to fix it to get the answer. To be able to do it with the real <script> content, you'll have to post the whole availabilityDiv which is probably too long. You can also try the suggestions in the edit in the answer.
when i replace them i get: SyntaxError: Unexpected end of JSON input btw when i log the availabilityDiv i get this if this will be useful: <ref *1> HTMLElement { parentNode: HTMLElement { parentNode: HTMLElement { parentNode: [HTMLElement], childNodes: [Array], nodeType: 1, rawTagName: 'x-wrapper-pdp', }, childNodes: [ [HTMLElement], [Circular *1], [HTMLElement] ], rawAttrs: '', nodeType: 1, rawTagName: 'div', //more lines childNodes: [ TextNode { parentNode: [Circular *1], childNodes: [], nodeType: 3, _rawText: '<![CDATA[{"layout":"cover","model":{"layout": //keep going

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.