CAPSOLVER
Blog
What is puppeteer and how to use in web scraping | Complete Guide 2024

What is puppeteer and how to use in web scraping | Complete Guide 2024

Logo of CapSolver

Lucas Mitchell

Automation Engineer

09-Jul-2024

Web scraping has become a crucial skill for anyone dealing with data extraction from the web. Whether you're a developer, data scientist, or enthusiast looking to gather information from websites, Puppeteer is one of the most powerful tools at your disposal. This complete guide will delve into what Puppeteer is and how to effectively use it in web scraping.

Introduction to Puppeteer

Puppeteer is a Node library that provides a high-level API to control Chrome or Chromium over the DevTools Protocol. It is maintained by the Google Chrome team and offers developers the ability to perform a variety of browser tasks such as generating screenshots, scraping websites, and most importantly, web scraping. Puppeteer is highly popular due to its headless browsing capabilities, meaning it can run without a graphical user interface, making it ideal for automated tasks.

Struggling with the repeated failure to completely solve the irritating captcha? Discover seamless automatic captcha solving with CapSolver AI-powered Auto Web Unblock technology!

Claim Your Bonus Code for top captcha solutions; CapSolver: WEBS. After redeeming it, you will get an extra 5% bonus after each recharge, Unlimited

Why Use Puppeteer for Web Scraping?

Axios and Cheerio are great choices for JavaScript web scraping, but they have limitations: handling dynamic content and circumventing anti-scraping mechanisms.

As a headless browser, Puppeteer excels at scraping dynamic content. It fully loads the target page, executes JavaScript, and can even trigger XHR requests to retrieve additional data. This is something static scrapers cannot achieve, especially with Single-Page Applications (SPAs) where the initial HTML lacks significant data.

What else can Puppeteer do? It can render images, capture screenshots, and has extension to solve various captcha like reCAPTCHA, Funcaptcha, hCaptcha . For example, you can program your script to navigate through a page, take screenshots at specific intervals, and analyze those images to gain competitive insights. The possibilities are virtually limitless!

Simple Use of Puppeteer

We previously completed first part of ScrapingClub using Selenium and Python. Now, let's use Puppeteer to complete part two

Before we start, make sure you have Puppeteer installed on your local machine. If not, you can install it using the following commands:

npm i puppeteer # Downloads compatible Chrome during installation.
npm i puppeteer-core # Alternatively, install as a library, without downloading Chrome.

Accessing a Webpage

const puppeteer = require('puppeteer');

(async function() {
    const browser = await puppeteer.launch({headless: false});
    const page = await browser.newPage();
    await page.goto('https://scrapingclub.com/exercise/detail_json/');

    // Pause for 5 seconds
    await new Promise(r => setTimeout(r, 5000));
    await browser.close();
})();

The puppeteer.launch method is used to launch a new Puppeteer instance and can accept a configuration object with multiple options. The most common one is headless, which specifies whether to run the browser in headless mode. If you do not specify this parameter, it defaults to true. Other common configuration options are as follows:

Parameter Type Default Value Description Example
args string[] Array of command-line arguments to pass when launching the browser args: ['--no-sandbox', '--disable-setuid-sandbox']
debuggingPort number Specifies the debugging port number to use debuggingPort: 8888
defaultViewport dict {width: 800, height: 600} Sets the default viewport size defaultViewport: {width: 1920, height: 1080}
devtools boolean false Whether to automatically open DevTools devtools: true
executablePath string Specifies the path to the browser executable executablePath: '/path/to/chrome'
headless boolean or 'shell' true Whether to run the browser in headless mode headless: false
userDataDir string Specifies the path to the user data directory userDataDir: '/path/to/user/data'
timeout number 30000 Timeout in milliseconds to wait for the browser to start timeout: 60000
ignoreHTTPSErrors boolean false Whether to ignore HTTPS errors ignoreHTTPSErrors: true

Setting the Window Size

To achieve the best browsing experience, we need to adjust two parameters: the viewport size and the browser window size. The code is as follows:

const puppeteer = require('puppeteer');

(async function() {
    const browser = await puppeteer.launch({
        headless: false,
        args: ['--window-size=1920,1080']
    });
    const page = await browser.newPage();
    await page.setViewport({width: 1920, height: 1080});
    await page.goto('https://scrapingclub.com/exercise/detail_json/');

    // Pause for 5 seconds
    await new Promise(r => setTimeout(r, 5000));
    await browser.close();
})();

Extracting Data

In Puppeteer, there are various methods to extract data.

  1. Using the evaluate Method

    The evaluate method executes JavaScript code in the browser context to extract the required data.

    const puppeteer = require('puppeteer');
    
    (async function () {
        const browser = await puppeteer.launch({
            headless: false,
            args: ['--window-size=1920,1080']
        });
        const page = await browser.newPage();
        await page.setViewport({width: 1920, height: 1080});
        await page.goto('https://scrapingclub.com/exercise/detail_json/');
    
        const data = await page.evaluate(() => {
            const image = document.querySelector('.card-img-top').src;
            const title = document.querySelector('.card-title').innerText;
            const price = document.querySelector('.card-price').innerText;
            const description = document.querySelector('.card-description').innerText;
            return {image, title, price, description};
        });
    
        console.log('Product name:', data.title);
        console.log('Product price:', data.price);
        console.log('Product image:', data.image);
        console.log('Product description:', data.description);
    
        // Pause for 5 seconds
        await new Promise(r => setTimeout(r, 5000));
        await browser.close();
    })();
  2. Using the $eval Method

    The $eval method selects a single element and extracts its content.

    const puppeteer = require('puppeteer');
    
    (async function () {
        const browser = await puppeteer.launch({
            headless: false,
            args: ['--window-size=1920,1080']
        });
        const page = await browser.newPage();
        await page.setViewport({width: 1920, height: 1080});
        await page.goto('https://scrapingclub.com/exercise/detail_json/');
    
        const title = await page.$eval('.card-title', el => el.innerText);
        const price = await page.$eval('.card-price', el => el.innerText);
        const image = await page.$eval('.card-img-top', el => el.src);
        const description = await page.$eval('.card-description', el => el.innerText);
    
        console.log('Product name:', title);
        console.log('Product price:', price);
        console.log('Product image:', image);
        console.log('Product description:', description);
    
        // Pause for 5 seconds
        await new Promise(r => setTimeout(r, 5000));
        await browser.close();
    })();
  3. Using the $$eval Method

    The $$eval method selects multiple elements at once and extracts their contents.

    const puppeteer = require('puppeteer');
    
    (async function () {
        const browser = await puppeteer.launch({
            headless: false,
            args: ['--window-size=1920,1080']
        });
        const page = await browser.newPage();
        await page.setViewport({width: 1920, height: 1080});
        await page.goto('https://scrapingclub.com/exercise/detail_json/');
    
        const data = await page.$$eval('.my-8.w-full.rounded.border > *', elements => {
            const image = elements[0].querySelector('img').src;
            const title = elements[1].querySelector('.card-title').innerText;
            const price = elements[1].querySelector('.card-price').innerText;
            const description = elements[1].querySelector('.card-description').innerText;
            return {image, title, price, description};
        });
    
        console.log('Product name:', data.title);
        console.log('Product price:', data.price);
        console.log('Product image:', data.image);
        console.log('Product description:', data.description);
    
        // Pause for 5 seconds
        await new Promise(r => setTimeout(r, 5000));
        await browser.close();
    })();
  4. Using page.$ and evaluate Methods

    The page.$ method selects elements, and the evaluate method executes JavaScript code in the browser context to extract data.

    const puppeteer = require('puppeteer');
    
    (async function () {
        const browser = await puppeteer.launch({
            headless: false,
            args: ['--window-size=1920,1080']
        });
        const page = await browser.newPage();
        await page.setViewport({width: 1920, height: 1080});
        await page.goto('https://scrapingclub.com/exercise/detail_json/');
    
        const imageElement = await page.$('.card-img-top');
        const titleElement = await page.$('.card-title');
        const priceElement = await page.$('.card-price');
        const descriptionElement = await page.$('.card-description');
        const image = await page.evaluate(el => el.src, imageElement);
        const title = await page.evaluate(el => el.innerText, titleElement);
        const price = await page.evaluate(el => el.innerText, priceElement);
        const description = await page.evaluate(el => el.innerText, descriptionElement);
    
        console.log('Product name:', title);
        console.log('Product price:', price);
        console.log('Product image:', image);
        console.log('Product description:', description);
    
        // Pause for 5 seconds
        await new Promise(r => setTimeout(r, 5000));
        await browser.close();
    })();

Get Around Anti-Scraping Protections

Completing the ScrapingClub exercises is relatively straightforward. However, in real-world data scraping scenarios, obtaining data is not always so easy. Some websites employ anti-scraping technologies that may detect your script as a bot and block it. The most common situation involves CAPTCHA challenges such as funcaptcha, datadome, recaptcha, hcaptcha, and geetest.

To solve these CAPTCHA challenges, one needs extensive experience in machine learning, reverse engineering, and browser fingerprinting countermeasures, which can take a significant amount of time.

Fortunately, you no longer need to handle all this work yourself. CapSolver provides a comprehensive solution that helps you easily solve all challenges. CapSolver offers a browser extension that allows you to automatically solve CAPTCHA challenges while using Puppeteer for data scraping. Additionally, it provides an API method to solve CAPTCHAs and obtain tokens. All of this can be done in just a few seconds. Check out this document to find out how to solve the various CAPTCHA correspondences you've encountered!

Conclusion

Web scraping is an invaluable skill for anyone involved in web data extraction, and Puppeteer, as a tool with an advanced API and powerful features, is one of the best choices for achieving this goal. Its ability to handle dynamic content and solve anti-scraping mechanisms makes it stand out from the crowd of scraping tools.

In this guide, we explore what Puppeteer is, its advantages in web scraping, and how to set it up and use it effectively. We demonstrate with examples how to access web pages, set viewport sizes, and extract data using various methods. In addition, we discuss the challenges posed by anti-capture technologies and how CapSolver provides a powerful solution to the CAPTCHA challenge.

Compliance Disclaimer: The information provided on this blog is for informational purposes only. CapSolver is committed to compliance with all applicable laws and regulations. The use of the CapSolver network for illegal, fraudulent, or abusive activities is strictly prohibited and will be investigated. Our captcha-solving solutions enhance user experience while ensuring 100% compliance in helping solve captcha difficulties during public data crawling. We encourage responsible use of our services. For more information, please visit our Terms of Service and Privacy Policy.

More

How to Solve CAPTCHA with Selenium and Node.js when Scraping
How to Solve CAPTCHA with Selenium and Node.js when Scraping

If you’re facing continuous CAPTCHA issues in your scraping efforts, consider using some tools and their advanced technology to ensure you have a reliable solution

The other captcha
Logo of CapSolver

Lucas Mitchell

15-Oct-2024

Solving 403 Forbidden Errors When Crawling Websites with Python
Solving 403 Forbidden Errors When Crawling Websites with Python

Learn how to overcome 403 Forbidden errors when crawling websites with Python. This guide covers IP rotation, user-agent spoofing, request throttling, authentication handling, and using headless browsers to bypass access restrictions and continue web scraping successfully.

The other captcha
Logo of CapSolver

Sora Fujimoto

01-Aug-2024

How to Use Selenium Driverless for Efficient Web Scraping
How to Use Selenium Driverless for Efficient Web Scraping

Learn how to use Selenium Driverless for efficient web scraping. This guide provides step-by-step instructions on setting up your environment, writing your first Selenium Driverless script, and handling dynamic content. Streamline your web scraping tasks by avoiding the complexities of traditional WebDriver management, making your data extraction process simpler, faster, and more portable.

The other captcha
Logo of CapSolver

Lucas Mitchell

01-Aug-2024

Scrapy vs. Selenium
Scrapy vs. Selenium: What's Best for Your Web Scraping Project

Discover the strengths and differences between Scrapy and Selenium for web scraping. Learn which tool suits your project best and how to handle challenges like CAPTCHAs.

The other captcha
Logo of CapSolver

Ethan Collins

24-Jul-2024

API vs Scraping
API vs Scraping : the best way to obtain the data

Understand the differences, pros, and cons of Web Scraping and API Scraping to choose the best data collection method. Explore CapSolver for bot challenge solutions.

The other captcha
Logo of CapSolver

Ethan Collins

15-Jul-2024

How to solve CAPTCHA With Selenium C#
How to solve CAPTCHA With Selenium C#

At the end of this tutorial, you'll have a solid understanding of How to solve CAPTCHA With Selenium C#

The other captcha
Logo of CapSolver

Rajinder Singh

10-Jul-2024