Download Instagram profile pic using Python


The Instagram profile pic can be downloaded using the Beautiful Soup and Request module in Python.Instagram is one of the most popular social media platforms that allows users to share photos and videos with their followers. As an Instagram user, you might come across some interesting profile pictures that you may want to save or download. In this article, we will download Instagram profile pics using Beautiful Soap and the Instaloader library in Python.

Method 1: Using Beautiful Soap and Request module

In this method, we send a GET request to the User's Instagram profile URL using the request module and it returns the HTML response. We need to parse the HTML response and extract the Profile picture of the user. This can be done using Beautiful Soap by finding the meta tag with the property attribute set to `og:image` Which contains the URL of the user profile picture in the content attribute.

Example

In the following example, instagram in the download_profile_pic('Instagram') function is the user name of the profile for which you want to download the profile pic.

import requests
from bs4 import BeautifulSoup

def download_profile_pic(username):
   # create the URL for the user's Instagram profile
   url = f"https://www.instagram.com/{username}/"

   # send a GET request to the URL and get the HTML response
   response = requests.get(url)

   # parse the HTML response using BeautifulSoup
   soup = BeautifulSoup(response.content, 'html.parser')

   # find the  tag containing the URL of the profile picture
   meta_tag = soup.find('meta', attrs={'property': 'og:image'})

   # extract the URL from the 'content' attribute of the  tag
   profile_pic_url = meta_tag['content']

   # create the filename for the downloaded profile picture
   file_name = f"{username}_profile_pic.jpg"

   # send a GET request to the profile picture URL and download the image
   pic_response = requests.get(profile_pic_url)
   with open(file_name, 'wb') as f:
      f.write(pic_response.content)

   print(f"Profile picture for {username} downloaded successfully!")

# example usage: download the profile picture of the user 'instagram'
download_profile_pic('instagram')

Output

Profile picture for rohit_the_lucifer downloaded successfully!

Method 2: Using Instaloader Library.

Before using the instaloader library we need to install the library using the pip command in Python.

pip install instaloader

Now to download an Instagram profile pic using the instaloader library import the instaloader library and then create an instance of Instaloader class and use the download_profile() function of the instaloader class to download the profile pic.

Example

In the below code enter the Instagram user name of the profile for which you want to download the profile pic.

import instaloader

# Create an instance of the Instaloader class
loader = instaloader.Instaloader()

dp = "instagram_user_name"

# Download the profile picture of the user
loader.download_profile(dp, profile_pic_only=True)

print(f"Profile picture for {dp} downloaded successfully!")

Output

Profile picture for instagram_user_name downloaded successfully!

Conclusion

In this article, we discussed how we can download Instagram profile pics using Beautiful Soap and the instaloader library in Python. The Beautiful Soap is used to parse the HTML content and get the URL of the profile pic to download it using the request module in Python.You can use this code to download the profile pictures of your friends, family, or even your favorite celebrities.

Updated on: 10-Jul-2023

924 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements