Can I load a local html file with the cheerio package in node.js?

后端 未结 3 2009
再見小時候
再見小時候 2020-12-28 14:19

I have a few html files on my harddrive that I\'d like to use jquery on to extract data from. Is this possible to do using cheerio? I\'ve tried giving cheerio the local pa

相关标签:
3条回答
  • 2020-12-28 14:34

    Extending damphat's answer to make it work for relative paths:

    import fs from 'fs';
    import path from 'path';
    
    const filePath = path.join(__dirname, './path/to/file.html');
    const $ = cheerio.load(fs.readFileSync(filePath));
    
    
    0 讨论(0)
  • 2020-12-28 14:37

    The input is an html string, so you need to read the html content yourself:

    var fs = require('fs');
    
    cheerio.load(fs.readFileSync('path/to/file.html'));
    
    0 讨论(0)
  • 2020-12-28 14:42

    A html file can be read asynchronously with the readFile function from the fs module. When the reading of the file finished the callback function is passed two arguments (err, data).

    The received data contains the html content and can be simply passed to the cheerio load function.

    var cheerio = require('cheerio');
    var fs = require('fs'); 
    
    fs.readFile('path/to/file.html', 'utf8', function(err, data) {
    
        if (err) throw err;
    
        var $ = cheerio.load(data);
        console.log($.html());
    });
    

    Sidenote: Because the encoding utf8 is specified as an optional second argument, the typeof data is a string. If the encoding is ommitted data will be a buffer. The load function understands this nonetheless, because the buffer is internally converted to a string with:

    if (Buffer.isBuffer(content))
      content = content.toString();
    

    Documentation of fs.readFile()

    0 讨论(0)
提交回复
热议问题