To download images from a page using Puppeteer, you can do the following steps:
npm install puppeteer
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
})();
const imageUrls = await page.evaluate(() => {
const images = Array.from(document.getElementsByTagName('img'));
return images.map(img => img.src);
});
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.
node yourscript.js
This will download all images from the specified page to the images
directory on your local machine.