How to find HTML elements by attribute using Cheerio?

How to find HTML elements by attribute using Cheerio? | ScrapingBee

This div has a data-example attribute with a value of 1

This div has a data-example attribute with a value of 2

This div does not have a data-example attribute

`;

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

// Find all div elements with a data-example attribute of “1” using the attribute selector
const divsWithAttribute = $(‘div[data-example=”1″]’);

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

// Output:
// This div has a data-example attribute with a value of 1
“>This div has a data-example attribute with a value of 1

This div has a data-example attribute with a value of 2

This div does not have a data-example attribute

`;

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

// Find all div elements with a data-example attribute of “1” using the attribute selector
const divsWithAttribute = $(‘div[data-example=”1″]’);

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

// Output:
// This div has a data-example attribute with a value of 1
“>This div has a data-example attribute with a value of 1

This div has a data-example attribute with a value of 2

This div does not have a data-example attribute

`;

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

// Find all div elements with a data-example attribute of “1” using the attribute selector
const divsWithAttribute = $(‘div[data-example=”1″]’);

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

// Output:
// This div has a data-example attribute with a value of 1
“>

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

Here’s some sample code that demonstrates how to find all div elements with a data-attribute of “example” using Cheerio:

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

This div has a data-example attribute with a value of 1

This div has a data-example attribute with a value of 2

This div does not have a data-example attribute

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

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 *