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

Jpaji Rajnish picture Jpaji Rajnish · Dec 18, 2013 · Viewed 16.6k times · Source

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

Answer

damphat picture damphat · Dec 18, 2013

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