我可以在node.js中使用cheerio包加载本地html文件吗? [英] Can I load a local html file with the cheerio package in node.js?

查看:124
本文介绍了我可以在node.js中使用cheerio包加载本地html文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的硬盘上有一些html文件,我想使用jquery从中提取数据.使用cheerio可以做到这一点吗?我尝试过为cheerio提供本地路径,但是它不起作用.我的一个想法是在节点中创建一个Web服务器,从html文件中读取内容,然后通过服务器将其通过管道传输到cheerio-这样

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

推荐答案

输入内容是一个html字符串,因此您需要自己阅读html内容:

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

这篇关于我可以在node.js中使用cheerio包加载本地html文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆