How to download an Instagram profile picture using Python

The profile photo is one of the main elements of any social media account, but apps like Instagram do not allow you to view or download it. This process can be easily accomplished using a web automation tool like Selenium with Python.


Learn how to use this powerful duo to interact with any element on a web page, automate it, and save yourself invaluable time investing in productive tasks. And the best part? Build this without even logging in or having an Instagram account!


The process of building algorithms

Algorithm building 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 username from a profile as input
  2. Open Google Chrome
  3. Visit Instagram profile
  4. Download profile picture

This serves as the algorithm of the problem statement.

This project uses the following Python modules and tools.

1. Urllib module

Urllib is a Python module used to handle Internet URLs. You are going to use this module to download the account’s profile picture from its source URL. If Urllib is not present on your system, you can install it using the command pip install urllib.

2. Time module

This module, although not required, can cause the build to fail if your Internet connection is slow or if the content of the web page does not load during the time the Python program interacts with the web page. The delay() function helps us to put in a little delay so that 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 compatible with various browsers including Google Chrome, Microsoft Edge, Safari, and Mozilla Firefox. To install Selenium in your Python environment, open your Terminal and run pip install selenium.

4.WebDriver

A webdriver is a tool used by Selenium that establishes a connection between the program and any website. There are different types of web controllers available depending on the browser you want to automate. For this build, you will be using 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. Please note the browser version.
  3. Visit the ChromeDriver – WebDriver for Chrome download page.
  4. Select the option that matches your version number of the current versions of ChromeDriver.
  5. Choose 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 will be useful for setting the path during encoding.

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

For any web automation process using Selenium and Python, a basic understanding of the web and its technologies is essential. The first step is to get an introduction to HTML followed by an understanding of Cascading Style Sheets (CSS). This is where you will become familiar with the concept of ids and classes.

Identifiers and classes are unique names given to an element or set of elements (tags) respectively. Using these locates the required element and instructs the Python program to target it specifically. To inspect the code and locate the profile picture:

  1. Open the web page of the Instagram account.
  2. Click on the browser Menu > More tools > Development tools or use the shortcut Ctrl + Shift + I to activate the Development tools view.
  3. Click and select the element picker (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 configured differently. Hover over the profile photo of a public account. The class attribute for the public profile is _aa8j.
  5. Repeat the previous step for a private profile. The class attribute is _aadp.

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

How to Build Instagram Profile Picture Downloader

Follow these steps to create the downloader.

  1. Import the necessary modules into the Python environment.
    from selenium import webdriver
    import time
    import urllib.request
  2. Using the input function, get the username of the profile whose profile picture is to be downloaded and store it in a variable called username.

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

    cd='chromedriver.exe'
  4. Use the webdriver.chrome function to start the Google Chrome browser.

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

    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. Set an optional recommended delay for the web page to fully load.

    time.sleep(5)
  8. Use the try-except block to locate and determine if the profile picture belongs to a public profile. This is done using the class attribute on the XPath expression. On failure, use the except block to fetch the profile picture of a private account.
    try:
    image=driver.find_element_by_xpath('//img[@class="_aa8j"]')
    except:
    image=driver.find_element_by_xpath('//img[@class="_aadp"]')
  9. Using get_attribute(), get the src attribute of the image. This returns the image link.

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

    path="D:\\"+username+".jpg"
  11. Download the image by passing the profile image link as the source and the local system folder path 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 show the path where the profile picture has been downloaded.

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

Final source code for Instagram profile photo downloader using Python

Putting it all together, you get:


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, it also ensures task completion and prevents errors. Use this technique to automate login to different websites, back up cloud servers, schedule messages, wish birthdays on social media platforms, create posts, post tweets and much more.

Leave a Comment