mercredi 2 mai 2018

Accessing script content with cheerio

I'm using Cheerio and request with Node.js to do some basic web scraping, but can't seem to figure out how to access the data. The page loads via request and I'm able to console.log the page title using Cheerio, but when I get to the scripts it's a complex mess of objects.

In the body section of the page it looks like..

<body> 
  <script src="someUrl" script type="text/javascript" /> 
  <script src="someUrl" script type="text/javascript" /> 
  <script src="someUrl" script type="text/javascript" />
  <script type="text/javascript">var months = [6,12,24,36,48,60]; var amounts = [5000,10000,15000,20000,25000]</script>

I'm trying to get to the variables in the last script to store them as variables in my node script for use, but I can't seem to access them, even as text.

When I try this in node, I get the page title followed by some huge object response in the console, not the variable text to parse. Suggestions?

$ = cheerio.load(body);
console.log($('title').text());
var text = $('script');
console.dir(text[3]);




Aucun commentaire:

Enregistrer un commentaire