Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

293
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda