Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Forgiving HTML/XML/RSS parser for node in pure JS
Pull request Compare This branch is 370 commits behind fb55:master.
Fetching latest commit...
Cannot retrieve the latest commit at this time.

htmlparser2 Build Status

A forgiving HTML/XML/RSS parser written in JS for NodeJS. The parser can handle streams (chunked data) and supports custom handlers for writing custom DOMs/output.


npm install htmlparser2


var htmlparser = require("htmlparser2");
var parser = new htmlparser.Parser({
    onopentag: function(name, attribs){
        if(name === "script" && attribs["language"] === "javascript"){
            console.log("JS! Hooray!");
    ontext: function(text){
        console.log("-->", text);
    onclosetag: function(tagname){
        if(tagname === "script"){
            console.log("That's it?!");
parser.write("Xyz <script language= javascript>var foo = '<<bar>>';< /  script>");

Output (simplified):

--> Xyz 
JS! Hooray!
--> var foo = '<<bar>>';
That's it?!

Read more about the parser in the wiki.

Get a DOM

The DomHandler (known as DefaultHandler in the original htmlparser module) produces a DOM (document object model) that may be manipulated using the DomUtils helper.

Read more about the DomHandler in the wiki.

Parsing RSS/RDF/Atom Feeds

new htmlparser.FeedHandler(function(<error> error, <object> feed){


Using a slightly modified version of node-expats bench.js, I received the following results (on a MacBook (late 2010):

The test may be found in tests/bench.js.

How is this different from node-htmlparser?

This is a fork of the project above. The main difference is that this is just intended to be used with node (it runs on other platforms using browserify). Besides, the code is much better structured, has less duplications and is remarkably faster than the original.

The parser now provides a callback interface close to sax.js (originally intended for readabilitySAX). I also fixed a couple of bugs & included some pull requests for the original project (eg. RDF feed support).

The support for location data and verbose output was removed a couple of versions ago. It's still available in the verbose branch.

The DefaultHandler and the RssHandler were renamed to clarify their purpose (to DomHandler and FeedHandler). The old names are still available when requiring htmlparser2, so your code should work as expected.

Something went wrong with that request. Please try again.