Tech

How to Download an Instagram Profile Pic Using Python


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


Learn to use this power duo to interact with any element of a webpage, 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 Algorithm Building Process

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

  1. Take the username of a profile as input
  2. Open Google Chrome
  3. Visit the Instagram profile
  4. Download the 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 URLs from the internet. You are going to use this module to download the profile picture of the account from its source URL. If Urllib is not present in your system, you can install it using the command pip install urllib.

2. Time Module

This module, while not mandatory, may cause the build to fail if your internet connection is slow or the contents of the webpage aren’t loaded during the time of Python program interaction with the webpage. The delay() function helps us put a small 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 supporting various browsers such as Google Chrome, Microsoft Edge, Safari, and Mozilla Firefox. To install Selenium in your Python environment, open your Terminal and execute pip install selenium.

4. WebDriver

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

  1. Check the version of the browser you’re using by visiting the Menu (3 dots) >Help > About Google Chrome.

  2. Note the version of the browser.

  3. Visit the downloads page of ChromeDriver – WebDriver for Chrome.
  4. Select the option that matches your version number from the current releases 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 helpful in setting the path during coding.

How to Inspect Code for Automating 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 gain an introduction to HTML followed by understanding Cascading Style Sheets (CSS). This is where you’ll get familiar with the concept of ids and classes.

Ids and classes are unique names given to an element or set of elements (tags) respectively. Using these 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 webpage of the Instagram account.
  2. Click on the browser Menu > More Tools > Developer Tools or use the shortcut Ctrl + Shift + I to activate the Developer Tools view.

  3. Click and select the Element Picker tool (mouse cursor icon) in the left corner of the window and hover it over any part of the webpage 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 the cursor over the profile pic of a public account. 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 procedure to understand any web page and target any element for automation.

How to Build the Instagram Profile Pic 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, gain 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 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 of the format https://www.instagram.com/ followed by the username. Set the URL of the profile as,

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

    driver.get(url_p)
  7. Set an optional recommended delay for the web page to load completely.

    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 by using the class attribute in the XPath expression. In case of failure, use the except block to search 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 the get_attribute(), gain the src attribute of the image. This returns the link of the image.

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

    path="D:\"+username+".jpg"
  11. Download the image by passing the link of the profile picture 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 display the path where the profile picture has been downloaded.

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

Final Source Code for Instagram Profile Pic Downloader Using Python

Bringing 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')

Applications of Web Automation

Automation not only helps you save time, money, and effort but also guarantees the completion of tasks while preventing errors. Use this technique to automate the login of different websites, perform cloud servers backup, schedule messages, wish birthdays on social media platforms, create posts, publish tweets, and many more.



Related Articles

Leave a Reply

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

Check Also
Close
Back to top button