How to Upload an Instagram Profile Picture with Python


Profile picture is one of the main elements of any social media account, but apps like Instagram don’t allow you to view or download it. This process can be easily done using a web automation tool like Selenium with Python.

Learn how to use this powerful duo to interact with any element of a web page, automate it and save valuable time by 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 should automate. The different steps required to upload a profile picture are:

  1. Take the username of a profile as input
  2. Open Google Chrome
  3. Visit Instagram Profile
  4. Upload profile picture

This serves as the problem statement algorithm.

This project uses the following Python modules and tools.

1. Urllib Module

Urllib is a Python module used to manage URLs from the Internet. You will use this module to download account 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, may cause the build to fail if your internet connection is slow or web page content is not loaded at the time the Python program interacts with the Web page. The delay() function helps us put a little delay so that the build doesn’t fail.

3. Selenium Modulus

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

4. Web Driver

A web driver is a tool used by Selenium that establishes 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 version, you will use the Google Chrome browser. To install the web driver for Chrome:

  1. Check the browser version you are using by visiting the Menu (3 points) >Help > About Google Chrome.
  2. Note the browser version.
  3. Visit the download page of ChromeDriver – WebDriver for Chrome.
  4. Select the option that matches your version number from the current ChromeDriver versions.
  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 while coding.

How to Inspect Code to Automate Any Aspect of a Web Page

For any web automation process using Selenium and Python, it is essential to have a basic understanding of the web and its technologies. 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 identifiers and classes.

Identifiers and classes are unique names assigned respectively to an element or to a set of elements (tags). By using them, you locate the required element and instruct the Python program to target it specifically. To inspect the code and locate the profile picture:

  1. Open the Instagram account webpage.
  2. Click on the Navigator Menu > More tools > development tools or use the shortcut Ctrl+Shift+I to activate the development tools see.
  3. Click and select the Item selector (mouse cursor icon) in the left corner of the window and place it over any part of the webpage to access that section of code.
  4. It is important to note that the profile pictures of a public account and a private account are defined differently. Hover over the profile picture of a public account. The class attribute for the public profile is _yy8j.
  5. Repeat the above step for a private profile. The class attribute is _aadp.

You can use this walkthrough 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. Using the input function, get the username of the profile whose profile picture should be uploaded 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 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 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. Set an optional recommended timeout 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 in the XPath expression. If that fails, use the except block to find 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 upload 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 uploaded. Optionally, you can also view the path where the profile picture was uploaded.

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

Final Source Code for Instagram Profile Picture 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 saves you time, money, and effort, but also ensures tasks get done while avoiding errors. Use this technique to automate connecting different websites, backing up cloud servers, scheduling messages, wishing birthdays on social media platforms, creating posts, posting tweets, and more.

Previous How to download Steam and install it on Windows
Next Rainbow Six Siege: System Requirements, Play & Download Guide