What is @types/cheerio?
@types/cheerio provides TypeScript type definitions for the Cheerio library, which is a fast, flexible, and lean implementation of core jQuery designed specifically for the server. Cheerio parses markup and provides an API for traversing/manipulating the resulting data structure.
What are @types/cheerio's main functionalities?
Loading HTML
Load HTML into Cheerio and parse it. This allows you to manipulate and traverse the HTML structure.
const cheerio = require('cheerio');
const $ = cheerio.load('<h2 class="title">Hello world</h2>');
console.log($('h2.title').text()); // Outputs: Hello world
Selecting Elements
Select elements using CSS selectors, similar to jQuery. This allows you to easily find and manipulate specific parts of the HTML.
const cheerio = require('cheerio');
const $ = cheerio.load('<ul><li>Apple</li><li>Banana</li></ul>');
$('li').each((index, element) => {
console.log($(element).text());
}); // Outputs: Apple, Banana
Modifying Elements
Modify elements in the HTML. This includes changing text, attributes, and more.
const cheerio = require('cheerio');
const $ = cheerio.load('<h2 class="title">Hello world</h2>');
$('h2.title').text('Hello there!');
console.log($.html()); // Outputs: <h2 class="title">Hello there!</h2>
Extracting Data
Extract data from the HTML. This is useful for web scraping and data extraction tasks.
const cheerio = require('cheerio');
const $ = cheerio.load('<ul><li class="fruit">Apple</li><li class="fruit">Banana</li></ul>');
const fruits = [];
$('li.fruit').each((index, element) => {
fruits.push($(element).text());
});
console.log(fruits); // Outputs: ['Apple', 'Banana']
Other packages similar to @types/cheerio
jsdom
jsdom is a JavaScript implementation of the DOM and HTML standards, primarily intended for use with Node.js. It provides a way to interact with and manipulate HTML documents in a way that is very similar to how you would in a browser. Compared to Cheerio, jsdom is more comprehensive and closer to a full browser environment, but it is also heavier and slower.
puppeteer
Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. It can be used for web scraping, automated testing, and more. Unlike Cheerio, Puppeteer operates a real browser, which means it can handle JavaScript-heavy websites, but it is also more resource-intensive.
selenium-webdriver
Selenium WebDriver is a tool for automating web application testing, and it can also be used for web scraping. It provides a way to control a browser programmatically. Compared to Cheerio, Selenium WebDriver is more powerful and can handle complex interactions, but it requires a running browser instance and is more complex to set up.
Installation
npm install --save @types/cheerio
Summary
This package contains type definitions for Cheerio (https://github.com/cheeriojs/cheerio).
Details
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/cheerio.
Additional Details
- Last updated: Sat, 20 Mar 2021 01:35:09 GMT
- Dependencies: @types/node
- Global values: none
Credits
These definitions were written by Bret Little, VILIC VANE, Wayne Maurer, Umar Nizamani, LiJinyao, Chennakrishna, AzSiAz, Ryo Ota, Hiroki Osame, and Artishevskiy Alexey.