Fahem Moz
Fahem Moz

Reputation: 327

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?

Upvotes: 3

Views: 5323

Answers (1)

Martin Ad&#225;mek
Martin Ad&#225;mek

Reputation: 18389

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).

Upvotes: 15

Related Questions