How to Download an Instagram Profile Pic Using Python

The profile picture is one of the most important elements of any social media account, but applications like Instagram don’t allow you to view or download it. This process can be easily accomplished using a web automation tool like Selenium with Python.


Learn to use this power duo to interact with every element of a webpage, automate it and save yourself valuable time that you could invest in productive tasks. And the best? Create this without signing up or having an Instagram account!


The algorithm creation process

Algorithm creation refers to the process of identifying the problem and listing the steps that the program needs to automate. The different steps required to download a profile picture are:

  1. Take a profile’s username as input
  2. Open Google Chrome
  3. Visit the Instagram profile
  4. Download the profile picture

This serves as the algorithm of the problem.

This project uses the following Python modules and tools.

1. Urllib module

Urllib is a Python module used to process URLs from the Internet. You will use this module to download the account’s profile picture from its source URL. If urllib doesn’t exist in your system, you can use the command to install it pip install urllib.

2. Time module

While this module is not mandatory, it can cause the build to fail if your internet connection is slow or the webpage content is not loading while the Python program is interacting with the webpage. The delay() function helps us to add a little delay so the build doesn’t fail.

3. Selenium module

One of the most popular open source browser automation tools is Selenium. It is available as a Python package that supports various browsers such as Google Chrome, Microsoft Edge, Safari, and Mozilla Firefox. To install Selenium in your Python environment, open your terminal and execute Install pip selenium.

4. Web Driver

A web driver is a tool used by Selenium that creates a connection between the program and any website. Different types of web drivers are available depending on the browser you want to automate. For this build you will use the Google Chrome browser. To install the web driver for Chrome:

  1. Check the version of the browser you are using by visiting the Menu (3 points) >help > About Google Chrome.
  2. Note the version of the browser.
  3. Visit the ChromeDriver – WebDriver for Chrome download page.
  4. From the latest versions of ChromeDriver, select the option that matches your version number.
  5. Select and download the file according to your operating system.
  6. Extract the downloaded file and place it in the same folder as your Python program. This is useful for setting the path while encoding.

How to review code to automate any aspect of a web page

A basic understanding of the web and its technologies is essential for any web automation process using Selenium and Python. The first step is to get an introduction to HTML, followed by understanding Cascading Style Sheets (CSS). Here you will be introduced to the concept of IDs and classes.

IDs and classes are unique names given to an element or a group of elements (tags). You use these to locate the required element and tell the Python program to target it. To review the code and find the profile picture:

  1. Open the Instagram account web page.
  2. Click on that browsers Menu > More tools > developer tools or use the shortcut Ctrl + Shift + I to activate it developer tools Outlook.
  3. Click and select the element selection tool (mouse cursor icon) in the left corner of the window and hover it over any part of the web page to jump to that section of code.
  4. It is important to note that the profile pictures of a public account and a private account are set differently. Hover over a public account profile picture. The class attribute for the public profile is _aa8j.
  5. Repeat the above step for a private profile. The class attribute is _aadp.

You can use this technique to understand any web page and target any element for automation.

How to create Instagram profile picture downloader

Follow these steps to create the downloader.

  1. Import the required modules into the Python environment.
    from selenium import webdriver
    import time
    import urllib.request
  2. Use the input function to determine the username of the profile whose profile picture you want to download and store it in a variable called username.

    username=input("Enter the username of the profile: ")
  3. Initialize the web driver by creating an object of it and passing in its file system path.

    cd='chromedriver.exe'
  4. Use the webdriver.Chrome Function to launch the Google Chrome browser.

    driver = webdriver.Chrome(cd)
  5. The URL of any Instagram account is in the format https://www.instagram.com/ followed by the username. Set the profile URL as,

    url='https://www.instagram.com/'
    url_p=url+user_h
  6. Pass the full URL of the Instagram profile to visit to the get() function.

    driver.get(url_p)
  7. Optionally set a recommended delay for the webpage to fully load.

    time.sleep(5)
  8. Use the try-except block to determine and determine if the profile picture belongs to a public profile. The class attribute in the XPath expression is used for this. In case of error, use the except block to search a private account profile picture.
    try:
    image=driver.find_element_by_xpath('//img[@class="_aa8j"]')
    except:
    image=driver.find_element_by_xpath('//img[@class="_aadp"]')
  9. Get the src attribute of the image with get_attribute(). This returns the link of the image.

    img_link=image.get_attribute('src')
  10. Set the path and extension of the downloaded file. For example, you can set the image to download to the D: drive of your file system in JPG format.

    path="D:\\"+username+".jpg"
  11. Download the picture by passing the link of the profile picture as the source and the path of the local system folder as the destination to the urlretrieve() function.

    urllib.request.urlretrieve(img_link,path)
  12. Visit the folder and see that the profile picture has been downloaded. Optionally, you can also display the path where the profile picture was downloaded.

    print("The profile pic has been downloaded at: "+path)

Final source code for Instagram Profile Pic Downloader with Python

Everything together results in:


from selenium import webdriver
import time
import urllib.request
user_h=input("Enter the username of the profile: ")
url='https://www.instagram.com/'
url_p=url+user_h
cd='chromedriver.exe'
driver = webdriver.Chrome(cd)
driver.get(url_p)
time.sleep(5)
try:
image=driver.find_element_by_xpath('//img[@class="_aa8j"]')
except:
image=driver.find_element_by_xpath('//img[@class="_aadp"]')


img_link=image.get_attribute('src')

Web Automation Applications

Automation not only helps you save time, money and effort, but also guarantees the completion of tasks and prevents errors. Use this technique to automate the login of different websites, perform cloud server backups, schedule messages, wish birthdays on social media platforms, create posts, tweet and much more.

Leave a Reply

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