How to find elements without specific attributes in Cheerio?

You can find elements without specific attributes in
Cheerio
by using the
:not CSS pseudo-class
and the attribute selector.

Here’s an example that demonstrates how to find all div elements without a class attribute using Cheerio:

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

This div has a class attribute

This div does not have a class attribute

`; // Load the HTML content into a Cheerio object const $ = cheerio.load(html); // Find all div elements without a class attribute using the :not pseudo-class and the attribute selector const divsWithoutClass = $('div:not([class])'); // Iterate over each div element without a class attribute and print its text content divsWithoutClass.each((i, div) => { console.log($(div).text()); }); // Output: // This div does not have a class attribute

Related Cheerio web scraping questions:

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 *