【发布时间】:2015-06-14 14:02:53
【问题描述】:
我正在使用 Node.js,我需要解析一个 html 文件。现在我使用了 htmlparser2 并在 parser.write("String") 方法中解析字符串。我可以使用 html 解析器解析 html 文件吗?如果是那怎么办?
感谢您的帮助?
【问题讨论】:
-
使用“fs”模块将文件作为字符串打开并传递给解析器。
标签: node.js html-parser
我正在使用 Node.js,我需要解析一个 html 文件。现在我使用了 htmlparser2 并在 parser.write("String") 方法中解析字符串。我可以使用 html 解析器解析 html 文件吗?如果是那怎么办?
感谢您的帮助?
【问题讨论】:
标签: node.js html-parser
var htmlparser = require("htmlparser2");
var parser = new htmlparser.Parser({
onopentag: function(name, attribs){
if(name === "script" && attribs.type === "text/javascript"){
console.log("JS! Hooray!");
}
},
ontext: function(text){
console.log("-->", text);
},
onclosetag: function(tagname){
if(tagname === "script"){
console.log("That's it?!");
}
}
}, {decodeEntities: true});
parser.write("Xyz <script type='text/javascript'>var foo = '<<bar>>';</script>");
parser.end();
【讨论】: