Signup/Sign In
PUBLISHED ON: MARCH 17, 2022

Instagram Photo Downloader Python Project

Instagram Photo Downloader Python Project

We'll learn how to use Python to retrieve photographs from Instagram in this lesson.

Prerequisites

Before we begin, we'll need to install the selenium, requests, and BeautifulSoup libraries on our system.

To install the commands on your system, type them one at a time into the terminal and push Enter.

For Windows:

pip install selenium
pip install requests
pip install bs4

For Mac:

sudo pip3 install selenium
sudo pip3 install requests
sudo pip3 install bs4

The chrome browser is used by the selenium library for online surfing, which necessitates the installation of chrome drivers on our machines

.Now that we have all of the necessary materials. Let's develop a python script to retrieve Instagram photographs from the command line.

Source Code for Instagram Photo Downloader Python Project

#python script to download instagram image

from bs4 import BeautifulSoup
import requests
from selenium import webdriver
import time

''' ask user to input the instagram post url '''
link = input("Enter Instagram Image URL: ")

''' 
create a webdriver chrome object by passing the path of "chromedriver.exe" file.(do not include .exe in the path).
'''
driver = webdriver.Chrome('chromedriver')

''' Open the instagram post on your chrome browser'''
driver.get(link)

''' Fetch the source file of the html page using BeautifulSoup'''
soup = BeautifulSoup(driver.page_source, 'lxml')

''' Extract the url of the image from the source code''' 
img = soup.find('img', class_='FFVAD')
img_url = img['src']


'''Download the image via the url using the requests library'''
r = requests.get(img_url)

with open("instagram"+str(time.time())+".png",'wb') as f: 
    f.write(r.content)

print('success')

Output

Instagram

Conclusion

We utilized time in the previous program. In addition to producing unique integers for our file name, we may use time().

The downloaded picture file will have a different name each time we execute the script.
In the above example, the picture is saved in the same directory as the python script.
The photographs on Instagram are created using JavaScript, which is originally concealed in the source code.

After all of the rendering has been completed, Selenium collects the source code of the web page. As a result, we get the whole HTML source file for the page, as well as the URL for the Instagram post's picture.



About the author:
Adarsh Kumar Singh is a technology writer with a passion for coding and programming. With years of experience in the technical field, he has established a reputation as a knowledgeable and insightful writer on a range of technical topics.