Parsing HTML in JavaScript (Node.js): Choosing a Tool for Working with Web Pages
Parsing HTML is one of the key tasks when developing web scrapers, content analyzers, and automated tests. In the Node.js ecosystem, there are several powerful libraries, each with its own strengths. In this article, we will look at three main tools: cheerio, jsdom, and htmlparser2, and also analyze when and which one is best to use.
1. Cheerio — jQuery for the Server
The cheerio library is the most popular choice for parsing HTML in Node.js. It provides a familiar jQuery-like syntax for navigating and manipulating the DOM tree. Cheerio does not create a full browser DOM but works with a lightweight abstraction, making it very fast.
Key Features:
- Support for CSS selectors (like in jQuery)
- Fast parsing without executing JavaScript
- Immutable methods (do not modify the original HTML)
- Work with attributes, text, and HTML structure
Usage Example:
const cheerio = require('cheerio');const html = '<div class="content"><h1>Hello, world!</h1><p>Article text</p></div>';
const $ = cheerio.load(html);const title = $('h1').text();const paragraphs = $('p').map((i, el) => $(el).text()).get();
console.log(title); // "Hello, world!"console.log(paragraphs); // ["Article text"]2. JSDOM — Full Browser in Node.js
If you need not only to parse static HTML but also to execute JavaScript scripts, emulate events, or work with modern APIs (e.g., fetch, localStorage), then jsdom is your choice. It creates a complete browser environment, providing maximum compatibility but requiring more resources.
When to use jsdom:
- Sites with dynamic content (SPA on React/Vue)
- Need to execute page scripts
- Testing web components
- Working with events and asynchronous operations
Parsing Example:
const { JSDOM } = require('jsdom');const html = '<html><body><div id="app">Content</div></body></html>';
const dom = new JSDOM(html);const document = dom.window.document;const content = document.getElementById('app').textContent;
console.log(content); // "Content"3. Htmlparser2 — Maximum Performance
The htmlparser2 library is a low-level parser that works on an event-driven basis (SAX-like approach). It does not create a DOM tree but generates events when encountering each tag, attribute, or text node. This makes it the fastest parser in Node.js.
Features:
- Streaming processing (can parse gigabyte-sized files)
- Minimal memory consumption
- Support for XML and HTML
- No built-in navigation methods (you need to write your own logic)
Usage Example:
const htmlparser2 = require('htmlparser2');const html = '<div class="item">Product 1</div><div class="item">Product 2</div>';
const items = [];const parser = new htmlparser2.Parser({ onopentag(name, attribs) { if (name === 'div' && attribs.class === 'item') { this.currentItem = ''; } }, ontext(text) { if (this.currentItem !== undefined) { this.currentItem += text; } }, onclosetag(name) { if (name === 'div' && this.currentItem !== undefined) { items.push(this.currentItem.trim() Also in library
Java Management Extensions (JMX) — Monitoring Java Applications
Internationalization (i18n) in TypeScript: i18next Library
OpenSSL C++ Guide: Complete Tutorial with Code Examples
JSON Processing in C# (.NET): Complete Guide to Newtonsoft.Json and System.Text.Json