Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
@types/cheerio
Advanced tools
@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.
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']
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 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 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.
npm install --save @types/cheerio
This package contains type definitions for Cheerio (https://github.com/cheeriojs/cheerio).
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/cheerio.
These definitions were written by Bret Little, VILIC VANE, Wayne Maurer, Umar Nizamani, LiJinyao, Chennakrishna, AzSiAz, Ryo Ota, Hiroki Osame, and Artishevskiy Alexey.
FAQs
TypeScript definitions for cheerio
We found that @types/cheerio demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.