Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing variable in script tag with jQuery/cheerio

I am using node.js + cheerio for web scraping.

After requesting the website, I get something like this.

<html>
    <head>
        ...
    </head>
    <body>
        <script>
           var x = {name: "Jeff"};
           var y = 4;
        </script>
    </body>
</html>  

How can I access the variable values through cheerio/jQuery?

like image 218
Fahem Moz Avatar asked Jul 13 '26 12:07

Fahem Moz


1 Answers

You could get the <script> tag contents as a text a find the variables via regexp:

const cheerio = require('cheerio');
const $ = cheerio.load(html); // your html

const text = $('script')[0].text(); // TODO there might be multiple script tags

// find variable `x` in the text
const matchX = text.match(/var x = (.*);/);
console.log(matchX[1]); // prints "{name: "Jeff"}"

// find variable `y` in the text
const matchY = text.match(/var y = (.*);/);
console.log(matchY[1]); // prints "4"

You can get string values like this. Then it depends on what you want to do, if you need those object values, you could use eval (but be aware that using eval can be dangerous), or you could parse it again via regexp or something (you probably know what values are you looking for).

like image 139
Martin Adámek Avatar answered Jul 15 '26 01:07

Martin Adámek