How to use JQuery selectors in Node.js

后端 未结 4 1528
梦谈多话
梦谈多话 2021-02-06 05:16

I\'m trying to extract email info from HTML files in my hard drive.

If I load the file in firefox and run jQuerify bookmarklet I can use successfully the following selec

4条回答
  •  天涯浪人
    2021-02-06 05:23

    Using Cheerio

    Cheerio is a server implementation of core jQuery that is perfect for using selectors.

    You can easily use the each function:

    $('a.iEmail').each(function (i, elem) {
      console.log($(this).attr('href'));
    });
    

    Full example:

    var fs = require('fs');
    var cheerio = require('cheerio');
    
    fs.readFile('file_1.html', 'utf-8', function (err, data) {
      if (err) {
        throw err;
      }
    
      var $ = cheerio.load(data);
    
      $('a.iEmail').each(function (i, elem) {
        console.log($(this).attr('href'));
      });
    });
    

提交回复
热议问题