Download Instagram Profile Photo using Python

In this tutorial, We will learn How to Download profile photos from Instagram using Username in Python. We will use the InstaloaderĀ Module of Python to download the profile photo from the username with proper error handling. This will take input of username from the user and Download the profile photo in the specific location as Profile_photo folder in D Drive and return the location of the photo. Here’s a step-by-step Guide.

Requirements:

Python 3.8 or above
pip install instaloader
pip install os
pip install shutil

Code:

# here imported all necessary modules
import instaloader
import os
import shutil

By importing the Instaloader module we will perform a download operation on user inputed username or profile name and by OS module we will create a folder on the loaction specificed in the code and by shutil module we will perform file handling operation.

# this is main function which help in downloading
def insta_profile_downloader(profile_name):
    try:
        initalize = instaloader.Instaloader()
        
        path = "D://profile_photo"

        try:
            if not os.path.exists(path):
                os.makedirs(path, exist_ok=True)

        except Exception:
            raise "folder access or creation error"
        
        try:
            initalize.download_profile(profile_name, profile_pic_only=True)

            cu = os.getcwd()
            download_loc = f"{cu}/{profile_name}"
            list_dir = os.listdir(download_loc)
            os.rename(f"{download_loc}/{list_dir[0]}", f"{download_loc}/{profile_name} profile photo.jpg")

            try:
                shutil.move(f"{download_loc}/{profile_name} profile photo.jpg", path)
                shutil.rmtree(download_loc)
            except Exception as e:
                print(f"error : {e}")
            return f"{path}"

        except Exception as e:
            print(f"error : {e}")

    except Exception as e:
        print(f"Error: {e}")

Here we have created a function with the name insta_profile_downloader which takes one argument i.e., profile_name. In this Code, we have first initialized the Instaloader class from the instaloader module for its use then we have provided a specific path (“D://profile_photo”) for storing the downloaded photo in the folder. Then we called the download_profile function of the Instaloader class using initialize as we have created it as an object for that class. we have given two arguments first is profile_name which contains the profile name of the user for which we have to download the photos and the second is profile_pic_only = True i.e., it will take only the profile photo from the profile of the user. then we have to use the os module to change the name of the file and then the shutil module to move the file to the specified location and delete the unnecessary file which was installed with the profile photo. In this code, we have applied proper error handling, and as the output it will return the path of the file stored.

if __name__ == "__main__":
    profile_name = input("enter the profile name : ").strip()

    down_loc = insta_profile_downloader(profile_name)
    print(f"{profile_name} profile photo downloaded successful")
    print(f"location of download is : - {down_loc}")

Here, we will take profile_name as the input from the user and call the function insta_profile_downloader as passed profile_name as the argument on download completion. It will show the download successful message and the path of the file where the profile photo is stored.

Complete Code:

import instaloader
import os 
import shutil

def insta_profile_downloader(profile_name):
    try:
        initalize = instaloader.Instaloader()
        
        path = "D://profile_photo"

        try:
            if not os.path.exists(path):
                os.makedirs(path, exist_ok=True)

        except Exception:
            raise "folder access or creation error"
        
        try:
            initalize.download_profile(profile_name, profile_pic_only=True)

            cu = os.getcwd()
            download_loc = f"{cu}/{profile_name}"
            list_dir = os.listdir(download_loc)
            os.rename(f"{download_loc}/{list_dir[0]}", f"{download_loc}/{profile_name} profile photo.jpg")

            try:
                shutil.move(f"{download_loc}/{profile_name} profile photo.jpg", path)
                shutil.rmtree(download_loc)
            except Exception as e:
                print(f"error : {e}")
            return f"{path}"

        except Exception as e:
            print(f"error : {e}")

    except Exception as e:
        print(f"Error: {e}")


if __name__ == "__main__":
    profile_name = input("enter the profile name : ").strip()

    down_loc = insta_profile_downloader(profile_name)
    print(f"{profile_name} profile photo downloaded successfully")
    print(f"location of download is : - {down_loc}")

This Python code used an instaloader module to download the profile photo using a username. The user has given the input of username/profile_name, and the code downloads the profile photo. If the specified folder is not created it will created automatically with the help of OS modules, other functions like renaming the file, and getting the current folder function are performed and shutil modules are used to move the file to the specific location as the download_profile function download the file at the same directory of the main file with some unnecessary files which was removed with the shutil module. This code handles exceptions to provide error feedback if something goes wrong while running the code and downloading the profile photos.

Output:

enter the profile name : Dev_pancholi02
Stored ID 52754301487 for profile dev_pancholi02.
dev_pancholi0223-06-24_17-17-06_UTC_profile_pic.jpg 
Dev_pancholi02 profile photo downloaded successfully
location of download is : - D://profile_photo

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top