Welcome folks today in this blog post we will be scraping images from url
using buffer and downloading
it inside the images
folder in javascript. All the full source code of the application is shown below.
Get Started
In order to get started you need to initialize a new node.js
project using the below command as shown below
npm init -y
npm i puppeteer
And after that you need to make the index.js
file and copy paste the following code
index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
const puppeteer = require('puppeteer'); const fs = require('fs'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); let counter = 0; page.on('response', async (response) => { const matches = /.*\.(jpg|png|svg|gif)$/.exec(response.url()); console.log(matches); if (matches && (matches.length === 2)) { const extension = matches[1]; const buffer = await response.buffer(); fs.writeFileSync(`images/image-${counter}.${extension}`, buffer, 'base64'); counter += 1; } }); await page.goto('https://www.bannerbear.com/solutions/automate-your-marketing/'); await browser.close(); })(); |
As you can see we are importing the puppeteer
library and then we are scraping all the images
from the url and then we are downloading the images
inside the images
folder using the buffer. Make sure that you make the images
folder before you execute the node.js
script as shown below
node index.js