Top Related Projects
The fast, flexible, and elegant library for parsing and manipulating HTML and XML.
A JavaScript implementation of various web standards, for use with Node.js
HTML parsing/serialization toolset for Node.js. WHATWG HTML Living Standard (aka HTML5)-compliant.
A very fast HTML parser, generating a simplified DOM, with basic element query support.
Quick Overview
htmlparser2 is a fast and forgiving HTML/XML parser written in JavaScript. It's designed to be efficient and handle malformed markup, making it suitable for parsing real-world HTML and XML documents. The library can be used in both Node.js and browser environments.
Pros
- High performance and efficiency compared to other parsers
- Tolerant of malformed HTML and XML
- Supports streaming, allowing parsing of large documents without loading them entirely into memory
- Actively maintained with regular updates and improvements
Cons
- Less feature-rich compared to some other parsing libraries
- May require additional libraries for more complex DOM manipulation tasks
- Documentation could be more comprehensive for advanced use cases
- Learning curve might be steeper for beginners compared to simpler parsing solutions
Code Examples
Parsing an HTML string:
const { Parser } = require("htmlparser2");
const parser = new Parser({
onopentag(name, attributes) {
console.log(`Open tag: ${name}`);
console.log("Attributes:", attributes);
},
ontext(text) {
console.log(`Text: ${text}`);
},
onclosetag(name) {
console.log(`Close tag: ${name}`);
},
});
parser.write("<div id='main'>Hello, world!</div>");
parser.end();
Streaming parse of a file:
const { Parser } = require("htmlparser2");
const fs = require("fs");
const parser = new Parser({
onopentag(name, attributes) {
if (name === "a" && attributes.href) {
console.log(`Found link: ${attributes.href}`);
}
},
});
const readStream = fs.createReadStream("example.html");
readStream.pipe(parser);
Creating a DOM:
const { parseDocument } = require("htmlparser2");
const { DomHandler } = require("domhandler");
const html = "<div><p>Hello, world!</p></div>";
const handler = new DomHandler((error, dom) => {
if (error) {
console.error(error);
} else {
console.log(dom);
}
});
const parser = new Parser(handler);
parser.write(html);
parser.end();
Getting Started
To use htmlparser2 in your project, first install it via npm:
npm install htmlparser2
Then, you can import and use it in your JavaScript code:
const { Parser } = require("htmlparser2");
const parser = new Parser({
onopentag(name, attributes) {
console.log(`Found tag: ${name}`);
},
ontext(text) {
console.log(`Found text: ${text}`);
},
onclosetag(name) {
console.log(`Closed tag: ${name}`);
},
});
parser.write("<div>Hello, world!</div>");
parser.end();
This basic example sets up a parser that logs information about tags and text content as it parses an HTML string.
Competitor Comparisons
The fast, flexible, and elegant library for parsing and manipulating HTML and XML.
Pros of Cheerio
- Higher-level API for DOM manipulation, similar to jQuery
- Easier to use for web scraping and parsing HTML
- Faster parsing and manipulation for most common use cases
Cons of Cheerio
- Less flexible for complex parsing scenarios
- Limited support for XML parsing
- Slightly larger package size
Code Comparison
Cheerio:
const $ = cheerio.load('<h2 class="title">Hello world</h2>');
$('h2.title').text('Hello there!');
$('h2').addClass('welcome');
htmlparser2:
const parser = new Parser({
onopentag(name, attribs) {
if (name === "h2" && attribs.class === "title") {
// Handle the h2 tag
}
}
});
parser.write('<h2 class="title">Hello world</h2>');
parser.end();
Summary
Cheerio provides a more user-friendly, jQuery-like API for HTML parsing and manipulation, making it ideal for web scraping and simple DOM operations. It's generally faster for common tasks but may be less suitable for complex parsing scenarios.
htmlparser2 offers a lower-level, event-driven approach, providing more flexibility for advanced parsing needs and better support for XML. It has a smaller package size but requires more code for basic operations.
Choose Cheerio for ease of use and familiarity with jQuery-like syntax, or htmlparser2 for more control over the parsing process and better performance in complex scenarios.
A JavaScript implementation of various web standards, for use with Node.js
Pros of jsdom
- Provides a full DOM implementation, simulating a browser environment
- Supports running client-side JavaScript within the simulated environment
- Offers a more comprehensive solution for web scraping and testing
Cons of jsdom
- Heavier and slower compared to lightweight parsing solutions
- May be overkill for simple HTML parsing tasks
- Requires more setup and configuration for basic use cases
Code Comparison
htmlparser2:
const parser = new Parser({
onopentag(name, attribs) {
console.log(`Found tag: ${name}`);
}
});
parser.write('<div id="main">');
parser.end();
jsdom:
const dom = new JSDOM(`<div id="main"></div>`);
console.log(dom.window.document.querySelector("#main").tagName);
Key Differences
- htmlparser2 is a lightweight, fast HTML parser
- jsdom provides a full DOM implementation with browser-like APIs
- htmlparser2 is better suited for simple parsing tasks
- jsdom is ideal for complex web scraping and testing scenarios requiring DOM manipulation
Both libraries have their strengths, and the choice depends on the specific requirements of your project. htmlparser2 excels in speed and simplicity, while jsdom offers a more comprehensive solution for browser-like environments.
HTML parsing/serialization toolset for Node.js. WHATWG HTML Living Standard (aka HTML5)-compliant.
Pros of parse5
- Full HTML5 spec compliance, including parsing of custom elements
- Supports both DOM and SAX-like parsing APIs
- Better error handling and recovery for malformed HTML
Cons of parse5
- Generally slower performance compared to htmlparser2
- Larger package size and memory footprint
- Steeper learning curve due to more complex API
Code Comparison
parse5:
const parse5 = require('parse5');
const document = parse5.parse('<html><body>Hello world!</body></html>');
console.log(document.childNodes[0].tagName); // 'html'
htmlparser2:
const htmlparser2 = require('htmlparser2');
const handler = new htmlparser2.DomHandler((error, dom) => {
console.log(dom[0].name); // 'html'
});
const parser = new htmlparser2.Parser(handler);
parser.write('<html><body>Hello world!</body></html>');
parser.end();
Both libraries offer HTML parsing capabilities, but parse5 focuses on full HTML5 compliance and a more comprehensive API, while htmlparser2 prioritizes speed and simplicity. parse5 is better suited for applications requiring strict adherence to the HTML5 spec, while htmlparser2 is ideal for scenarios where performance is critical and full spec compliance is not necessary.
A very fast HTML parser, generating a simplified DOM, with basic element query support.
Pros of node-html-parser
- Lightweight and fast, with a smaller footprint than htmlparser2
- Simpler API, making it easier to use for basic parsing tasks
- Built-in DOM manipulation methods, reducing the need for additional libraries
Cons of node-html-parser
- Less comprehensive parsing capabilities compared to htmlparser2
- May not handle complex or malformed HTML as well as htmlparser2
- Smaller community and fewer updates, potentially leading to slower bug fixes
Code Comparison
node-html-parser:
const parser = require('node-html-parser');
const root = parser.parse('<ul id="list"><li>Hello World</li></ul>');
console.log(root.firstChild.structure);
htmlparser2:
const htmlparser2 = require('htmlparser2');
const dom = htmlparser2.parseDocument('<ul id="list"><li>Hello World</li></ul>');
console.log(dom.children[0].children[0].children[0].data);
Both libraries offer HTML parsing capabilities, but node-html-parser provides a more straightforward API for simple tasks. htmlparser2, on the other hand, offers more advanced features and better handles complex HTML structures. The choice between the two depends on the specific requirements of your project, with node-html-parser being suitable for simpler parsing needs and htmlparser2 for more comprehensive parsing tasks.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
htmlparser2
The fast & forgiving HTML/XML parser.
htmlparser2 is the fastest HTML parser, and takes some shortcuts to get there. If you need strict HTML spec compliance, have a look at parse5.
Installation
npm install htmlparser2
A live demo of htmlparser2 is available on AST Explorer.
Ecosystem
| Name | Description |
|---|---|
| htmlparser2 | Fast & forgiving HTML/XML parser |
| domhandler | Handler for htmlparser2 that turns documents into a DOM |
| domutils | Utilities for working with domhandler's DOM |
| css-select | CSS selector engine, compatible with domhandler's DOM |
| cheerio | The jQuery API for domhandler's DOM |
| dom-serializer | Serializer for domhandler's DOM |
Usage
htmlparser2 itself provides a callback interface that allows consumption of documents with minimal allocations.
For a more ergonomic experience, read Getting a DOM below.
import * as htmlparser2 from "htmlparser2";
const parser = new htmlparser2.Parser({
onopentag(name, attributes) {
/*
* This fires when a new tag is opened.
*
* If you don't need an aggregated `attributes` object,
* have a look at the `onopentagname` and `onattribute` events.
*/
if (name === "script" && attributes.type === "text/javascript") {
console.log("JS! Hooray!");
}
},
ontext(text) {
/*
* Fires whenever a section of text was processed.
*
* Note that this can fire at any point within text and you might
* have to stitch together multiple pieces.
*/
console.log("-->", text);
},
onclosetag(tagname) {
/*
* Fires when a tag is closed.
*
* You can rely on this event only firing when you have received an
* equivalent opening tag before. Closing tags without corresponding
* opening tags will be ignored.
*/
if (tagname === "script") {
console.log("That's it?!");
}
},
});
parser.write(
"Xyz <script type='text/javascript'>const foo = '<<bar>>';</script>",
);
parser.end();
Output (with multiple text events combined):
--> Xyz
JS! Hooray!
--> const foo = '<<bar>>';
That's it?!
Parser events
All callbacks are optional. The handler object you pass to Parser may implement any subset of these:
| Event | Description |
|---|---|
onopentag(name, attribs, isImplied) | Opening tag. attribs is an object mapping attribute names to values. isImplied is true when the tag was opened implicitly (HTML mode only). |
onopentagname(name) | Emitted for the tag name as soon as it is available (before attributes are parsed). |
onattribute(name, value, quote) | Attribute. quote is " / ' / null (unquoted) / undefined (no value, e.g. disabled). |
onclosetag(name, isImplied) | Closing tag. isImplied is true when the tag was closed implicitly (HTML mode only). |
ontext(data) | Text content. May fire multiple times for a single text node. |
oncomment(data) | Comment (content between <!-- and -->). |
oncdatastart() | Opening of a CDATA section (<![CDATA[). |
oncdataend() | End of a CDATA section (]]>). |
onprocessinginstruction(name, data) | Processing instruction (e.g. <?xml ...?>). |
oncommentend() | Fires after a comment has ended. |
onparserinit(parser) | Fires when the parser is initialized or reset. |
onreset() | Fires when parser.reset() is called. |
onend() | Fires when parsing is complete. |
onerror(error) | Fires on error. |
Parser options
| Option | Type | Default | Description |
|---|---|---|---|
xmlMode | boolean | false | Treat the document as XML. This affects entity decoding, self-closing tags, CDATA handling, and more. Set this to true for XML, RSS, Atom and RDF feeds. |
decodeEntities | boolean | true | Decode HTML entities (e.g. & -> &). |
lowerCaseTags | boolean | !xmlMode | Lowercase tag names. |
lowerCaseAttributeNames | boolean | !xmlMode | Lowercase attribute names. |
recognizeSelfClosing | boolean | xmlMode | Recognize self-closing tags (e.g. <br/>). Always enabled in xmlMode. |
recognizeCDATA | boolean | xmlMode | Recognize CDATA sections as text. Always enabled in xmlMode. |
Usage with streams
While the Parser interface closely resembles Node.js streams, it's not a 100% match.
Use the WritableStream interface to process a streaming input:
import { WritableStream } from "htmlparser2/WritableStream";
const parserStream = new WritableStream({
ontext(text) {
console.log("Streaming:", text);
},
});
const htmlStream = fs.createReadStream("./my-file.html");
htmlStream.pipe(parserStream).on("finish", () => console.log("done"));
Getting a DOM
The parseDocument helper parses a string and returns a DOM tree (a Document node).
import * as htmlparser2 from "htmlparser2";
const dom = htmlparser2.parseDocument(
`<ul id="fruits">
<li class="apple">Apple</li>
<li class="orange">Orange</li>
</ul>`,
);
parseDocument accepts an optional second argument with both parser and DOM handler options:
const dom = htmlparser2.parseDocument(data, {
// Parser options
xmlMode: true,
// domhandler options
withStartIndices: true, // Add `startIndex` to each node
withEndIndices: true, // Add `endIndex` to each node
});
Searching the DOM
The DomUtils module (re-exported on the main htmlparser2 export) provides helpers for finding nodes:
import * as htmlparser2 from "htmlparser2";
const dom = htmlparser2.parseDocument(`<div><p id="greeting">Hello</p></div>`);
// Find elements by ID, tag name, or class
const greeting = htmlparser2.DomUtils.getElementById("greeting", dom);
const paragraphs = htmlparser2.DomUtils.getElementsByTagName("p", dom);
// Find elements with custom test functions
const all = htmlparser2.DomUtils.findAll(
(el) => el.attribs?.class === "active",
dom,
);
// Get text content
htmlparser2.DomUtils.textContent(greeting); // "Hello"
For CSS selector queries, use css-select:
import { selectAll, selectOne } from "css-select";
const results = selectAll("ul#fruits > li", dom);
const first = selectOne("li.apple", dom);
Or, if you'd prefer a jQuery-like API, use cheerio.
Modifying and serializing the DOM
Use DomUtils to modify the tree, and dom-serializer (also available as DomUtils.getOuterHTML) to serialize it back to HTML:
import * as htmlparser2 from "htmlparser2";
const dom = htmlparser2.parseDocument(
`<ul><li>Apple</li><li>Orange</li></ul>`,
);
// Remove the first <li>
const items = htmlparser2.DomUtils.getElementsByTagName("li", dom);
htmlparser2.DomUtils.removeElement(items[0]);
// Serialize back to HTML
const html = htmlparser2.DomUtils.getOuterHTML(dom);
// "<ul><li>Orange</li></ul>"
Other manipulation helpers include appendChild, prependChild, append, prepend, and replaceElement -- see the domutils docs for the full API.
Parsing feeds
htmlparser2 makes it easy to parse RSS, RDF and Atom feeds, by providing a parseFeed method:
const feed = htmlparser2.parseFeed(content);
This returns an object with type, title, link, description, updated, author, and items (an array of feed entries), or null if the document isn't a recognized feed format.
The xmlMode option is enabled by default for parseFeed. If you pass custom options, make sure to include xmlMode: true.
Performance
After having some artificial benchmarks for some time, @AndreasMadsen published his htmlparser-benchmark, which benchmarks HTML parses based on real-world websites.
At the time of writing, the latest versions of all supported parsers show the following performance characteristics on GitHub Actions (sourced from here):
htmlparser2 : 2.17215 ms/file ± 3.81587
node-html-parser : 2.35983 ms/file ± 1.54487
html5parser : 2.43468 ms/file ± 2.81501
neutron-html5parser: 2.61356 ms/file ± 1.70324
htmlparser2-dom : 3.09034 ms/file ± 4.77033
html-dom-parser : 3.56804 ms/file ± 5.15621
libxmljs : 4.07490 ms/file ± 2.99869
htmljs-parser : 6.15812 ms/file ± 7.52497
parse5 : 9.70406 ms/file ± 6.74872
htmlparser : 15.0596 ms/file ± 89.0826
html-parser : 28.6282 ms/file ± 22.6652
saxes : 45.7921 ms/file ± 128.691
html5 : 120.844 ms/file ± 153.944
Security contact information
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.
Top Related Projects
The fast, flexible, and elegant library for parsing and manipulating HTML and XML.
A JavaScript implementation of various web standards, for use with Node.js
HTML parsing/serialization toolset for Node.js. WHATWG HTML Living Standard (aka HTML5)-compliant.
A very fast HTML parser, generating a simplified DOM, with basic element query support.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot