我正在使用 Node.js,我需要解析一个 html 文件。现在我使用了 htmlparser2,它在 parser.write("String") 方法中解析字符串。我可以使用 html 解析器解析 html 文件吗?如果是那么怎么办?
感谢帮助?
最佳答案
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();
关于node.js - 我如何使用 htmlparser2 来解析 html 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30830286/