Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cheerio script element - data

Im using cheerio to do some scraping and want to access the head js element on a page (notably instructables). I can access it but it comes back as function.

So using this:

  console.log($('script').attr('type', "application/ld+json").text);
  • (on this: http://www.instructables.com/id/Making-an-online-Fish-Tank-webcam!/step3/Cut-the-project-box/)
  • this provides 'Function'

  • I thought strinify would work but it doesn't :(

like image 447
willwade Avatar asked Jun 19 '26 08:06

willwade


1 Answers

This is because $("script").attr("type","application/ld+json"); returns an array of script tags (there's more than only one script tag on the page) and it's also changing the type of all script tag of the page to application/ld+json

See JQuery .attr() documentation.

If you need to get the one with that type $("script[type='application/ld+json']") will do the trick.

var request = require('request');
var cheerio = require('cheerio');

request('http://www.instructables.com/id/Making-an-online-Fish-Tank-webcam!/step3/Cut-the-project-box/', function (error, response, html) {
  if (!error && response.statusCode == 200) {
    var $ = cheerio.load(html);
    var obj = $("script[type='application/ld+json']"); 

    for(var i in obj){
        for(var j in obj[i].children){
            var data = obj[i].children[j].data;
            if(data){
               console.log(data);
            }
        }
    }
  }
});
like image 109
Antoine Thiry Avatar answered Jun 20 '26 23:06

Antoine Thiry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!