I have created a puppeteer script to scrape some images from a website. I'm storing the images urls inside an array and I want to save each images using fs
. How I can grab the image and it's name from the url to pass it to the writeFileSync
function?
this is an example of the url for images https://www.examplesite.com/wp-content/uploads/2016/08/cat-500x500.jpg
let images = [];
page.on('response', (response) => {
const url = response.url();
if( url.startsWith('https://www.examplesite.com/wp-content/uploads/') ){
images.push(url);
console.log(images);
}
});
page.goto('https://www.examplesite.com/shop/?product_count=150', {waitUntil: ['load', 'networkidle2']});
page.waitForNavigation().then( () => {
page.goto('https://www.examplesite.com/shop/page/2/?product_count=150', {waitUntil: ['load', 'networkidle2']})
.then( () => {
images.forEach( (img) => {
// I'm trying to save the image by passing the url to the fs.writeFileSync but without success.
fs.writeFileSync(www, img);
});
});
});