Skip to main content

Cheerio is a very popular tool for parsing HTML and XML in NodeJs. It is fast, flexible, and easy to use. Since it implements a subset of JQuery, it's easy to start using Cheerio if you're already familiar with JQuery.

Here is how to search for HTML code with Cheerio in NodeJs.

Searching by elements

To search for particular HTML code using Cheerio in Node.js, you can use the $ function to load the HTML string or file and then use CSS selectors to find the desired element or elements.

Here's an example of how to search for a specific HTML code using Cheerio in Node.js:

const cheerio = require('cheerio');
const html = '<html><body><h1>Hello World</h1></body></html>';

const $ = cheerio.load(html);
const element = $('h1');

console.log(element.html());

In this example, we load the HTML string into Cheerio using the cheerio.load() function, and then use the CSS selector $('h1') to find the h1 element.

We can then access the HTML content of the element using the html() function. The above code will output "Hello World" to the console.

You can also use more complex CSS selectors to search for elements with specific attributes, classes, or IDs. Here's an example:

const cheerio = require('cheerio');
const html = '<html><body><div class="container"><h1>Hello World</h1></div></body></html>';

const $ = cheerio.load(html);
const element = $('.container h1');

console.log(element.html());

In this example, we use the CSS selector $('.container h1') to find the h1 element inside a div element with the class container. This code will also output "Hello World" to the console.

Searching by code

To search for a specific HTML code, you can use Cheerio's .html() function to get the HTML code of the entire document, and then search for the desired code using string manipulation or regular expressions.

Here's an example of how to search for a specific HTML code using Cheerio in Node.js:

const cheerio = require('cheerio');
const html = '<html><body><div><div></div></div></body></html>';

const $ = cheerio.load(html);
const codeToFind = '<div><div></div></div>';
const found = $.html().includes(codeToFind);

console.log(found);

In this example, we load the HTML string into Cheerio using the cheerio.load() function, and then use the .html() function to get the HTML code of the entire document.

We then use the includes() function to check if the desired HTML code is present in the document. The above code will output true to the console, since the codeToFind string '<div><div></div></div>' is present in the HTML code.

Note that this method will also match any whitespace or formatting differences in the HTML code. If you need to match the code exactly, you can use regular expressions or other string manipulation methods to remove whitespace and formatting before comparing the strings.