Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 248
Jpaji Rajnish Avatar asked Dec 18 '13 17:12

Jpaji Rajnish


People also ask

Can we connect html with node js?

Using Clean architecture for Node. For sending larger code, we definitely require to have a separate file for html code. Response object gives a sendFile() function to return a html file to client. How to provide path to html file in sendFile() ? We import the path core module of node.

How do I send html content in node JS?

Use res. sendFile instead of reading the file manually so express can handle setting the content-type properly for you. You don't need the app. engine line, as that is handled internally by express.


1 Answers

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')); 
like image 158
damphat Avatar answered Oct 09 '22 13:10

damphat