Reputation: 145
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)
Upvotes: 1
Views: 1348
Reputation: 24928
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.
Upvotes: 1