How can I use htmlparser2 to parse html files?

I am using Node.js and I need to parse an html file. Now I have used htmlparser2 and parses the string in the parser.write ("String") method. Can i parse html file using html parser? If so, how?

Help appreciated?

+3


source to share


1 answer


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

-five


source







All Articles