Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Download Instagram profile pic using 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 Soup and the Instaloader library in Python.
Method 1: Using Beautiful Soup and Requests Module
In this method, we send a GET request to the user's Instagram profile URL using the requests 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 Soup 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 username 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 <meta> 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 <meta> 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 instagram downloaded successfully!
Method 2: Using Instaloader Library
Before using the instaloader library we need to install it 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 to download the profile pic.
Example
In the below code enter the Instagram username of the profile for which you want to download the profile pic ?
import instaloader
# Create an instance of the Instaloader class
loader = instaloader.Instaloader()
username = "instagram"
# Download the profile picture of the user
loader.download_profile(username, profile_pic_only=True)
print(f"Profile picture for {username} downloaded successfully!")
Output
Profile picture for instagram downloaded successfully!
Comparison
| Method | Dependencies | Complexity | Best For |
|---|---|---|---|
| Beautiful Soup + Requests | requests, beautifulsoup4 | Medium | Learning web scraping |
| Instaloader | instaloader | Low | Quick downloads |
Conclusion
In this article, we discussed how to download Instagram profile pics using Beautiful Soup and the Instaloader library in Python. The Instaloader method is simpler and more reliable, while the Beautiful Soup approach helps you understand web scraping fundamentals.
