Cheerio: Fast and Flexible HTML/XML Parsing and Manipulation Library

Summary
Cheerio is a popular library for parsing and manipulating HTML and XML documents in Node.js. It provides a jQuery-like API, making it easy to select, traverse, and modify elements with proven syntax. Known for its blazingly fast performance and incredible flexibility, Cheerio is an excellent choice for web scraping and server-side DOM manipulation.
Repository Info
Tags
Click on any tag to explore related repositories
Introdução
Cheerio is a lightweight and efficient library designed for parsing and manipulating HTML and XML documents. It brings a familiar jQuery-like syntax to the server-side, allowing developers to interact with the DOM without the overhead of a full browser environment. Built with speed and flexibility in mind, Cheerio is an ideal tool for tasks such as web scraping, content parsing, and server-side rendering. It leverages parse5 for robust HTML parsing and can optionally use htmlparser2.
Instalação
To get started with Cheerio, you can install it using your preferred package manager:
npm install cheerio
# or
bun add cheerio
Exemplos
Here's a quick example demonstrating how to load HTML, manipulate elements, and render the result:
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>
This snippet shows how easy it is to select elements using CSS selectors, modify their text content, add classes, and then retrieve the updated HTML.
Porquê usar
Cheerio stands out for several compelling reasons:
- Proven Syntax: It implements a subset of core jQuery, offering a familiar and powerful API without browser inconsistencies.
- Blazingly Fast: With a simple and consistent DOM model, Cheerio ensures incredibly efficient parsing, manipulation, and rendering.
- Incredibly Flexible: Wrapping around
parse5and optionallyhtmlparser2, Cheerio can parse nearly any HTML or XML document and works seamlessly in both browser and server environments.
Links
- GitHub Repository: https://github.com/cheeriojs/cheerio
- Chinese Readme (????): https://github.com/cheeriojs/cheerio/wiki/Chinese-README
- Cheerio in Production Wiki: https://github.com/cheeriojs/cheerio/wiki/Cheerio-in-Production