Download Instagram Images with Node.js and Puppeteer
22 March 20244 min read
This article explain how to use Google Puppeteer and download images from a Instagram using Puppeteer.
Downloading images from Instagram using Node.js and Puppeteer involves automating the process of navigating to Instagram, accessing the desired images, and saving them to your local machine. Here's a basic example of how you can achieve this:
Let's download images from Instagram of Kim Kardashian (@kimkardashian).
What is Puppeteer?
Puppeteer is a Node.js library developed by Google that provides a high-level API over the Chrome DevTools Protocol. It allows you to control and automate Chromium or Chrome browser instances, enabling tasks such as web scraping, automated testing, taking screenshots, generating PDFs, and more.
Puppeteer provides a powerful set of features for interacting with web pages programmatically.
Setup Application
Step 1: Install Dependencies
First, you need to create Puppeteer config file and install a library:
Create file .puppeteerrc.cjs
now run
npm install puppeteerAdd to your package.json file:
Step 2: Test the Puppeteteer
We will attempt to create a screenshot using Puppeteer of a random post by Kim Kardashian (https://www.instagram.com/kimkardashian/p/C4lwwOYSpW-/?hl=en&img_index=1).
Create a JavaScript file, for example, downloadInstagramImages.js, and write the script to check if the puppeteteer is working properly:
Now run the code:
node downloadInstagramImages.jsWe got this in our screen.png file:
Step 3: Create Helpers Functions
We need to create two functions: one to download an image from a source link and another to check if our destination folder already exists.
Check if the destination folder already exists function
You can also use another method to resolve a directory
Download function
Add new imports at the top of the file:
Step 4: Write the Run Function
Step 5: Run the Script
Run the script using Node.js:
node downloadInstagramImages.jsHere is a complete example of the script:
Conclusion:
Using Puppeteer, you can automate the process of downloading images from Instagram. However, keep in mind the legal and ethical considerations involved when accessing and downloading content from websites.