cheerio vs domino vs html-dom-parser vs htmlparser2 vs jsdom vs parse5
HTML Parsing Libraries
cheeriodominohtml-dom-parserhtmlparser2jsdomparse5Similar Packages:

HTML Parsing Libraries

HTML parsing libraries are essential tools in web development that allow developers to manipulate and traverse HTML documents programmatically. These libraries provide various functionalities for parsing HTML strings, creating DOM-like structures, and enabling easy querying and manipulation of HTML elements. They are widely used for web scraping, server-side rendering, and transforming HTML content in Node.js applications.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
cheerio030,1251.01 MB34a month agoMIT
domino0788-376 years agoBSD-2-Clause
html-dom-parser01061.37 MB419 days agoMIT
htmlparser204,810306 kB21a month agoMIT
jsdom021,5043.4 MB42410 days agoMIT
parse503,873337 kB347 months agoMIT

Feature Comparison: cheerio vs domino vs html-dom-parser vs htmlparser2 vs jsdom vs parse5

Performance

  • cheerio:

    Cheerio is optimized for speed and efficiency, making it an excellent choice for web scraping and manipulating large HTML documents quickly. It operates in memory and avoids the overhead of a full browser environment, which enhances performance.

  • domino:

    Domino is lightweight and provides a minimalistic DOM implementation, resulting in good performance for server-side rendering tasks. However, it may not be as fast as Cheerio for extensive DOM manipulations due to its simpler API.

  • html-dom-parser:

    html-dom-parser is designed for simplicity and ease of use, which may come at the cost of performance for very large documents. It is best suited for smaller HTML strings where speed is not the primary concern.

  • htmlparser2:

    htmlparser2 is highly efficient and can handle large documents with ease. It supports streaming parsing, allowing for better memory management and performance when dealing with extensive HTML content.

  • jsdom:

    jsdom provides a full simulation of a browser environment, which can introduce some performance overhead compared to lighter libraries. However, it is still performant enough for most testing scenarios and complex DOM manipulations.

  • parse5:

    parse5 is known for its speed and compliance with HTML standards, making it suitable for applications that require accurate parsing of HTML documents. It is optimized for performance and can handle large documents effectively.

API and Usability

  • cheerio:

    Cheerio offers a jQuery-like API that is intuitive and easy to use, allowing developers to quickly manipulate the DOM using familiar syntax. This makes it accessible for those who have experience with jQuery.

  • domino:

    Domino provides a simple API that mimics the browser's DOM, making it easy for developers to create and manipulate elements. However, it lacks some advanced features found in other libraries.

  • html-dom-parser:

    html-dom-parser has a straightforward API that is easy to understand, making it suitable for beginners. It allows for quick parsing of HTML strings without complex configurations.

  • htmlparser2:

    htmlparser2 has a more complex API that offers fine-grained control over the parsing process. While it may have a steeper learning curve, it provides powerful features for advanced users.

  • jsdom:

    jsdom has a comprehensive API that closely resembles the browser's DOM API, making it suitable for developers familiar with client-side JavaScript. It supports many browser features, enhancing usability for testing.

  • parse5:

    parse5 has a well-defined API that focuses on standards compliance, making it easy to use for developers who need to parse and serialize HTML documents accurately.

Error Handling

  • cheerio:

    Cheerio does not perform extensive error handling for malformed HTML, which can lead to unexpected results if the input is not well-formed. It is best used with clean HTML content.

  • domino:

    Domino is forgiving with malformed HTML but may not handle all edge cases perfectly. It is designed to create a DOM structure from imperfect HTML, making it suitable for server-side rendering.

  • html-dom-parser:

    html-dom-parser is designed for simplicity and may not provide robust error handling for malformed HTML. It is best used with well-formed HTML strings to avoid issues during parsing.

  • htmlparser2:

    htmlparser2 excels at handling malformed HTML and provides detailed error reporting, making it a reliable choice for projects that need to parse unpredictable HTML content.

  • jsdom:

    jsdom is designed to simulate a browser environment and includes error handling for various DOM manipulations. It can handle malformed HTML reasonably well, making it suitable for testing scenarios.

  • parse5:

    parse5 is built to be standards-compliant and handles malformed HTML gracefully. It provides detailed error messages, making it a good choice for applications that require accurate parsing.

Compatibility

  • cheerio:

    Cheerio is compatible with Node.js and works well with various HTML formats. It does not support browser-specific features, focusing instead on server-side usage.

  • domino:

    Domino is designed for server-side rendering and is compatible with Node.js. It does not provide browser-specific features, making it suitable for lightweight DOM manipulations.

  • html-dom-parser:

    html-dom-parser is compatible with Node.js and can be used in browser environments as well. It is designed for basic HTML parsing tasks without complex dependencies.

  • htmlparser2:

    htmlparser2 is compatible with Node.js and can be used in both server-side and client-side applications. It is versatile and can handle various HTML formats effectively.

  • jsdom:

    jsdom is designed to work in Node.js and provides a comprehensive simulation of a browser environment. It supports many web standards, making it suitable for testing client-side code.

  • parse5:

    parse5 is compatible with Node.js and can be used in both server-side and client-side applications. It adheres to HTML standards, ensuring compatibility with various HTML formats.

Use Cases

  • cheerio:

    Cheerio is ideal for web scraping, where fast parsing and manipulation of HTML documents are required. It is commonly used in projects that need to extract data from web pages efficiently.

  • domino:

    Domino is suitable for server-side rendering of HTML content, allowing developers to create and manipulate DOM elements without a full browser environment. It is useful for generating dynamic HTML on the server.

  • html-dom-parser:

    html-dom-parser is best for simple HTML parsing tasks where ease of use is prioritized. It is suitable for projects that require quick parsing of HTML strings without complex features.

  • htmlparser2:

    htmlparser2 is well-suited for applications that need to parse and manipulate large or malformed HTML documents. It is often used in web crawlers and data extraction tools.

  • jsdom:

    jsdom is perfect for testing client-side JavaScript code in a Node.js environment. It allows developers to run scripts that rely on a browser-like DOM, making it ideal for unit testing.

  • parse5:

    parse5 is used in applications that require accurate parsing and serialization of HTML documents. It is often employed in web frameworks and tools that need to handle HTML content reliably.

How to Choose: cheerio vs domino vs html-dom-parser vs htmlparser2 vs jsdom vs parse5

  • cheerio:

    Choose Cheerio if you need a fast and lightweight library for parsing HTML and manipulating the DOM with a jQuery-like syntax. It is ideal for web scraping tasks where performance is critical and you want to use familiar jQuery methods.

  • domino:

    Opt for Domino if you require a minimalistic DOM implementation that closely resembles the browser's DOM. It is useful for server-side rendering and provides a straightforward way to create and manipulate DOM elements without the overhead of a full browser environment.

  • html-dom-parser:

    Select html-dom-parser if you want a simple and straightforward way to parse HTML strings into a DOM-like structure. It is easy to use and suitable for basic HTML parsing needs without additional features or complexity.

  • htmlparser2:

    Use htmlparser2 if you need a highly customizable and efficient HTML parser that can handle malformed HTML. It is well-suited for projects that require fine-grained control over the parsing process and supports streaming parsing for large documents.

  • jsdom:

    Choose jsdom if you need a full-fledged DOM and HTML environment that simulates a web browser. It is particularly useful for testing and running scripts that rely on a browser-like environment, making it ideal for unit testing client-side code in Node.js.

  • parse5:

    Select parse5 if you require a fast, standards-compliant HTML parser that can handle both HTML and XML. It is designed for high performance and is suitable for applications that need to parse and serialize HTML documents accurately.

README for cheerio

cheerio

The fast, flexible, and elegant library for parsing and manipulating HTML and XML.

中文文档 (Chinese Readme)

import * as cheerio from 'cheerio';
const $ = cheerio.load('<h2 class="title">Hello world</h2>');

$('h2.title').text('Hello there!');
$('h2').addClass('welcome');

$.html();
//=> <html><head></head><body><h2 class="title welcome">Hello there!</h2></body></html>

Installation

Install Cheerio using a package manager like npm, yarn, or bun.

npm install cheerio
# or
bun add cheerio

Features

❤ Proven syntax: Cheerio implements a subset of core jQuery. Cheerio removes all the DOM inconsistencies and browser cruft from the jQuery library, revealing its truly gorgeous API.

ϟ Blazingly fast: Cheerio works with a very simple, consistent DOM model. As a result parsing, manipulating, and rendering are incredibly efficient.

❁ Incredibly flexible: Cheerio wraps around parse5 for parsing HTML and can optionally use the forgiving htmlparser2. Cheerio can parse nearly any HTML or XML document. Cheerio works in both browser and server environments.

API

Loading

First you need to load in the HTML. This step in jQuery is implicit, since jQuery operates on the one, baked-in DOM. With Cheerio, we need to pass in the HTML document.

// ESM or TypeScript:
import * as cheerio from 'cheerio';

// In other environments:
const cheerio = require('cheerio');

const $ = cheerio.load('<ul id="fruits">...</ul>');

$.html();
//=> <html><head></head><body><ul id="fruits">...</ul></body></html>

Selectors

Once you've loaded the HTML, you can use jQuery-style selectors to find elements within the document.

$( selector, [context], [root] )

selector searches within the context scope which searches within the root scope. selector and context can be a string expression, DOM Element, array of DOM elements, or cheerio object. root, if provided, is typically the HTML document string.

This selector method is the starting point for traversing and manipulating the document. Like in jQuery, it's the primary method for selecting elements in the document.

$('.apple', '#fruits').text();
//=> Apple

$('ul .pear').attr('class');
//=> pear

$('li[class=orange]').html();
//=> Orange

Rendering

When you're ready to render the document, you can call the html method on the "root" selection:

$.root().html();
//=>  <html>
//      <head></head>
//      <body>
//        <ul id="fruits">
//          <li class="apple">Apple</li>
//          <li class="orange">Orange</li>
//          <li class="pear">Pear</li>
//        </ul>
//      </body>
//    </html>

If you want to render the outerHTML of a selection, you can use the outerHTML prop:

$('.pear').prop('outerHTML');
//=> <li class="pear">Pear</li>

You may also render the text content of a Cheerio object using the text method:

const $ = cheerio.load('This is <em>content</em>.');
$('body').text();
//=> This is content.

The "DOM Node" object

Cheerio collections are made up of objects that bear some resemblance to browser-based DOM nodes. You can expect them to define the following properties:

  • tagName
  • parentNode
  • previousSibling
  • nextSibling
  • nodeValue
  • firstChild
  • childNodes
  • lastChild

Screencasts

https://vimeo.com/31950192

This video tutorial is a follow-up to Nettut's "How to Scrape Web Pages with Node.js and jQuery", using cheerio instead of JSDOM + jQuery. This video shows how easy it is to use cheerio and how much faster cheerio is than JSDOM + jQuery.

Cheerio in the real world

Are you using cheerio in production? Add it to the wiki!

Sponsors

Does your company use Cheerio in production? Please consider sponsoring this project! Your help will allow maintainers to dedicate more time and resources to its development and support.

Headlining Sponsors

Tidelift Github AirBnB HasData

Other Sponsors

OnlineCasinosSpelen Nieuwe-Casinos.net

Backers

Become a backer to show your support for Cheerio and help us maintain and improve this open source project.

Vasy Kafidoff

License

MIT