我如何使用htmlparser2parsinghtml文件?

我正在使用Node.js,我需要parsing一个HTML文件。 现在我已经使用了htmlparser2,它parsingparser.write(“String”)方法中的string。 我可以使用htmlparsing器parsing一个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(); 

https://github.com/fb55/htmlparser2

http://demos.forbeslindesay.co.uk/htmlparser2/