How to find HTML elements by multiple tags with Cheerio?

You can find HTML elements by multiple tags in
Cheerio
by separating the tag selectors with a ,.

Here’s some sample code that demonstrates how to find all div and span elements using Cheerio:

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

This is a div element

This is a span element

This is another div element

`; // Load the HTML content into a Cheerio object const $ = cheerio.load(html); // Find all div and span elements const divsAndSpans = $('div, span'); // Iterate over each div and span element and print its text content divsAndSpans.each((i, element) => { console.log($(element).text()); }); // Output: // This is a div element // This is a span element // This is another div element

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 *