'I need to get a script using cherrio (not get text) [duplicate]
I am scrapping a page and I need to get a json from a script tag the script tag seens like
<template data-type="json" data-varname="__STATE__">
<script>
{
"someJson": here
}
</script>
</template>
I got the value with something like this using jquery in browser
$('template[data-varname="__STATE__"]').content.children[0]
and i got the value with something like that on my node code
const listItems = await $('template[data-varname="__STATE__"]');
await fs.promises.writeFile('site.html', listItems.html())
but my return is only a script like this
<script>
{
"someJson": here
}
</script>
inside a html file but I would like to get the json and already be able to use
Solution 1:[1]
I got what I wanted as follows
const listItems = await $('template[data-varname="__STATE__"]');
const scriptItem = await listItems.html()
const removeScript = scriptItem.replace("<script>","").replace("</script>","")
const jsonFromScript = JSON.parse(removeScript)
However this is not a solution that I like, if anyone knows a more "correct" way to do it I would be grateful
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Fernando Moura |
