2015-06-14 139 views
3

我正在使用Node.js,我需要解析一个html文件。现在我已经使用了htmlparser2,它解析parser.write(“String”)方法中的字符串。我可以使用html解析器解析一个html文件吗?如果是,那么如何?如何使用htmlparser2解析html文件?

帮助表示赞赏?

回答

-5
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/

+0

原来的问题询问如何将HTML文件喂到解析器(没有GET请求)。 – dman