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

杀马特。学长 韩版系。学妹 提交于 2019-12-31 10:32:11

问题


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 path but it doesn't work. One idea I had would be to create a web server in node, read from the html file, and then pipe it to cheerio through the server - would this


回答1:


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'));



回答2:


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()



来源:https://stackoverflow.com/questions/20664841/can-i-load-a-local-html-file-with-the-cheerio-package-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!