Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

292
Views
I need to get a script using cherrio (not get text)

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!