How to find HTML elements by class with Cheerio?

How to find HTML elements by class with Cheerio? | ScrapingBee

This div has a class of example

This div also has a class of example

This div does not have a class of example

`;

// Load the HTML content into a Cheerio object
const $ = cheerio.load(html);

// Find all div elements with a class of “example” using the class selector
const divsWithClass = $(‘div.example’);

// Iterate over each div element with a class of “example” and print its text content
divsWithClass.each((i, div) => {
console.log($(div).text());
});

// Output:
// This div has a class of example
// This div also has a class of example
“>This div has a class of example

This div also has a class of example

This div does not have a class of example

`;

// Load the HTML content into a Cheerio object
const $ = cheerio.load(html);

// Find all div elements with a class of “example” using the class selector
const divsWithClass = $(‘div.example’);

// Iterate over each div element with a class of “example” and print its text content
divsWithClass.each((i, div) => {
console.log($(div).text());
});

// Output:
// This div has a class of example
// This div also has a class of example
“>This div has a class of example

This div also has a class of example

This div does not have a class of example

`;

// Load the HTML content into a Cheerio object
const $ = cheerio.load(html);

// Find all div elements with a class of “example” using the class selector
const divsWithClass = $(‘div.example’);

// Iterate over each div element with a class of “example” and print its text content
divsWithClass.each((i, div) => {
console.log($(div).text());
});

// Output:
// This div has a class of example
// This div also has a class of example
“>

You can find HTML elements by class in
Cheerio
by using the class selector.

Here’s some sample code that demonstrates how to find all div elements with a class of example using Cheerio:

const cheerio = require('cheerio');
const html = `
  

This div has a class of example

This div also has a class of example

This div does not have a class of example

`; // Load the HTML content into a Cheerio object const $ = cheerio.load(html); // Find all div elements with a class of "example" using the class selector const divsWithClass = $('div.example'); // Iterate over each div element with a class of "example" and print its text content divsWithClass.each((i, div) => { console.log($(div).text()); }); // Output: // This div has a class of example // This div also has a class of example

Related Cheerio web scraping questions:

Tired of getting blocked while scraping the web?

ScrapingBee API handles headless browsers and rotates proxies for you.

Get access to 1,000 free API credits, no credit card required!

ScrapingBee API handles headless browsers and rotates proxies for you.

Copyright © 2025

Made in France

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *