How to download images from a page using Puppeteer?

To download images from a page using Puppeteer, you can do the following steps:

  1. First, you need to install Puppeteer by running the following command in your terminal:
npm install puppeteer
  1. Create a JavaScript file and require Puppeteer:
const puppeteer = require('puppeteer');
  1. Initialize Puppeteer and navigate to the page you want to download images from:
(async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://example.com'); })();
  1. Next, you can use Puppeteer to extract the URLs of the images on the page by evaluating JavaScript in the context of the page:
const imageUrls = await page.evaluate(() => { const images = Array.from(document.getElementsByTagName('img')); return images.map(img => img.src); });
  1. Finally, you can use the download module from npm to download the images to a specified directory on your local machine:
const download = require('download'); const fs = require('fs'); for (const url of imageUrls) { const response = await download(url); fs.writeFileSync(`./images/${url.split('/').pop()}`, response); }

Make sure to create an images directory in the same directory as your JavaScript file before running the script.

  1. Run the script in your terminal:
node yourscript.js

This will download all images from the specified page to the images directory on your local machine.