2015-06-14 135 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