Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use the function css() of jQuery on NodeJS

I'm currently using NodeJS to scrap a web page with the following modules: Cheerio and Request

Is it possible to use the css() jQuery function in NodeJS ?

request({uri: website}, function(error, response, body) {

  var $ = cheerio.load(body);
  myItemColor = $('.myitem').css('color');
  console.log(myItemColor);

});

I would like to retrieve a css property of a given jQuery object.

Thanks for your help!

like image 472
Antoine Avatar asked May 13 '26 09:05

Antoine


1 Answers

Yes, it is.

But you need to run jQuery inside a DOM so that this works. Check out my jQuery on the server sample, especially this part of the app.js file to see how you can use jsdom to create a server-side DOM for a download HTML site.

PS: I haven't tried it, but it should work with the css function as well (I don't see any reason why it shouldn't).

like image 126
Golo Roden Avatar answered May 16 '26 01:05

Golo Roden