How do you create a Cheerio object from a string of HTML?

Cheerio is a fast, flexible, and lean implementation of core jQuery designed specifically for the server to parse, manipulate, and render HTML. To create a Cheerio object from a string of HTML, you need to first install Cheerio using npm (if you haven't already), and then use the cheerio.load() function to load the HTML string into Cheerio, which creates a Cheerio instance.

Here's how you can do it in JavaScript (Node.js):

  1. Install Cheerio (if not already installed):
npm install cheerio
  1. Use Cheerio to load the HTML string:
const cheerio = require('cheerio');

// Your HTML string
const htmlString = `
  <html>
  <head>
    <title>Sample Page</title>
  </head>
  <body>
    <h1>Hello, world!</h1>
  </body>
  </html>
`;

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

// Now you can use the Cheerio object to manipulate and query the HTML
console.log($('h1').text()); // Output: Hello, world!

After loading the HTML string with cheerio.load(), you can use the returned function ($ in this case) to select elements using the familiar jQuery syntax. The $ function provides a jQuery-like interface for traversing and manipulating the parsed HTML structure.

You can also make changes to the HTML by using the methods provided by Cheerio. Here's an example of how to change the text of the h1 tag:

$('h1').text('Goodbye, world!');
console.log($.html());  // Output the modified HTML

With Cheerio, you can perform most of the operations that you might be familiar with from client-side jQuery, such as selecting elements, getting attributes, iterating over collections, and modifying the DOM structure. The main difference is that Cheerio does this on the server side, without a DOM or browser environment.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon