Cheerio and Puppeteer are powerful libraries designed for Node.js (a backend JavaScript runtime environment) that enable web scraping capabilities. However, they have distinct differences that are crucial to understand when selecting the right tool for your specific project requirements.
In this comprehensive web scraping guide, we’ll analyze Cheerio versus Puppeteer to help you make an informed decision for your web scraping needs. Additionally, we’ll demonstrate how to construct a web scraper using both Cheerio and Puppeteer, complete with detailed code examples. Let’s dive in!
What is Cheerio?
Cheerio is a Node.js framework that processes raw HTML and XML documents and creates a consistent DOM model for traversing and manipulating the resulting data structure. It enables element selection through CSS and XPath selectors, simplifying DOM navigation.
Cheerio’s primary advantage lies in its exceptional speed. Since Cheerio operates without rendering websites like a traditional browser (it skips CSS application and external resource loading), it remains lightweight and fast. While the performance difference might be negligible in smaller projects, it becomes a significant time-saver for large-scale scraping operations.
What is Puppeteer?
Conversely, Puppeteer functions as a browser automation tool, specifically designed to simulate user behavior for website and web application testing. It “provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol.”
For web scraping purposes, Puppeteer equips your script with full browser engine capabilities, enabling you to scrape pages requiring JavaScript execution (such as SPAs), handle infinite scrolling, extract dynamic content, and much more.
Cheerio vs. Puppeteer: Differences and When to Use Them
Before exploring the specifics of each library, here’s a comparative overview of Cheerio and Puppeteer:
Cheerio
- Cheerio was built with web scraping in mind.
- It’s a DOM parser, able to parser HTML and XML files.
- Cheerio can’t interact with the site or access content behind scripts.
- It has an easy learning curve thanks to its simple syntax.
- Cheerio is lightning fast in comparison to Puppeteer.
- Cheerio makes extracting data super simple using JQuery like syntax and CSS/XPath selectors to navigate the DOM.
Puppeteer
- Puppeteer was designed for browser automation and testing
- It can execute Javascript, making it able to scrape dynamic pages like single-page applications (SPAs).
- Puppeteer can interact with websites, accessing content behind login forms and scripts.
- It has a steep learning curve as it has more functionalities and requires Async for better results.
- Compared to Cheerio, Puppeteer is quite slow.
- Puppeteer can take screenshots, submit forms and make PDFs.
Now that you have a comprehensive understanding, let’s examine what each library provides and how you can leverage them to extract valuable data from websites.
Should You Use Cheerio or Puppeteer for Web Scraping?
While you may already have an understanding of optimal scenarios, let us clarify any uncertainties. Cheerio excels when scraping static pages that don’t require interactions such as clicks, JavaScript rendering, or form submissions. However, if a website employs JavaScript to dynamically inject content, Puppeteer becomes necessary.
Our recommendation stems from the fact that Puppeteer represents overkill for static websites. Cheerio enables faster scraping of more pages with cleaner, more concise code.
Nevertheless, numerous scenarios benefit from combining both libraries. After all, Cheerio simplifies parsing and element selection, while Puppeteer provides access to script-protected content and automates events like infinite scroll pagination.
How to Build a Scraper with Cheerio and Puppeteer [Code Example]
To ensure this example is easy to follow, we’ll construct a scraper using both Puppeteer and Cheerio that navigates to https://quotes.toscrape.com/ and extracts all quotes and authors from the first page.
Scraper with Cheerio and Puppeteer
Step 1. Installing Node.js, Cheerio, and Puppeteer
We’ll download Node.js from the official website and follow the installation instructions. Next, create a new project folder (we named it ‘cheerio-puppeteer-project’) and open it in VScode – or your preferred editor. Within your project folder, open a terminal and execute npm init -y to initialize your project.
Step 2. Open the Target Website Using Puppeteer
Now we’re ready to install dependencies using npm install cheerio puppeteer. After installation completes, create a new file called ‘index.js’ and import our dependencies at the beginning.
const puppeteer = require('puppeteer');
const cheerio = require('cheerio');
Next, we’ll create an empty array named scraped_quotes to store our results, followed by our async function to access the await operator. We’ll include a browser.close() method at the function’s end.
scraped_quotes = [];
(async () => {
await browser.close();
});
Using Puppeteer, let’s launch a browser instance, open a new page, and navigate to our target website.
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://quotes.toscrape.com/');
Parsing the HTML with Cheerio
To access the website’s HTML, we use evaluate and return the raw HTML data – this step is essential because Cheerio only works with HTML or XML data, requiring access before parsing.
const pageData = await page.evaluate(() => {
return {
html: document.documentElement.innerHTML,
};
});
For testing purposes, you can use console.log(pageData) to output the response to your terminal. Since we know it works, we’ll pass the raw HTML to Cheerio for parsing.
const $ = cheerio.load(pageData.html);
Now, we can use $ to reference the parsed HTML file version throughout our project.
Step 3. Selecting Elements with Cheerio
Before writing our code, we must understand the page structure. Navigate to the page in your browser and inspect the quote-containing cards.
We can observe that our target elements are contained within divs with the class “quote”. We can select them and iterate through all divs to extract the quote text and author.
After inspecting these elements, here are our targets:
- Divs containing our target elements:
$('div.quote') - Quote text:
$(element).find('span.text') - Quote author:
$(element).find('.author')
Let’s translate this into code:
let quote_cards = $('div.quote');
quote_cards.each((index, element) => {
quote = $(element).find('span.text').text();
author = $(element).find('.author').text();
});
Using the text() method allows us to access the element’s text content instead of returning the HTML string.
Step 4. Pushing the Scraped Data Into a Formatted List
If we console.log() our data at this point, it would appear as unstructured text. Instead, we’ll utilize the empty array created outside our function and push the data there. Add these lines to your script immediately after your author variable:
scraped_quotes.push({
'Quote': quote,
'By': author,
})
Full Web Scraper Code Built with Cheerio and Puppeteer
With everything in place, we can console.log(scraped_quotes) before closing the browser:
//dependencies
const puppeteer = require('puppeteer');
const cheerio = require('cheerio');
//empty list to store our data
scraped_quotes = [];
//main function for our scraper
(async () => {
//launching and opening our page
const browser = await puppeteer.launch();
const page = await browser.newPage();
//navigating to a URL
await page.goto('https://quotes.toscrape.com/');
//getting access to the raw HTML
const pageData = await page.evaluate(() => {
return {
html: document.documentElement.innerHTML,
};
});
//parsing the HTML and picking our elements
const $ = cheerio.load(pageData.html);
let quote_cards = $('div.quote');
quote_cards.each((index, element) => {
quote = $(element).find('span.text').text();
author = $(element).find('.author').text();
//pushing our data into a formatted list
scraped_quotes.push({
'Quote': quote,
'By': author,
})
});
//console logging the results
console.log(scraped_quotes);
//closing the browser
await browser.close();
})();
This results in a properly formatted data array:
Cheerio vs. Puppeteer in Web Scraping: Both Win
We hope you found this comprehensive overview of two of the most effective web scraping tools available for JavaScript/Node.js valuable. While Cheerio is typically preferable over Puppeteer in most scenarios, Puppeteer provides essential additional capabilities for complex projects.
We’ve created more detailed Cheerio and Puppeteer tutorials for beginners. You can explore these libraries further through our resources.
However, you can also leverage Webparsers to reduce code complexity through our JavaScript rendering capabilities. By simply setting the render=true parameter within the request, Webparsers will render the page before returning the raw HTML data for Cheerio to process.
Webparsers helps accelerate development time and prevents your scripts from being blocked by sophisticated anti-scraping mechanisms such as browser profiling and CAPTCHAs through automated IP rotation, CAPTCHA handling, and utilizing years of statistical analysis to determine optimal headers for each request.
You can sign up for Webparsers and receive 5000 free API credits to launch your project.
Until next time, happy scraping!