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:
Take the username of a profile as input Open Google Chrome Visit the Instagram profile Download the profile picture
This serves as the algorithm of the problem statement.
Understanding and Installing the Required Modules and Tools
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:
Check the version of the browser you’re using by visiting the Menu (3 dots) >Help > About Google Chrome. Note the version of the browser. Visit the downloads page of ChromeDriver - WebDriver for Chrome. Select the option that matches your version number from the current releases of ChromeDriver. Choose and download the file according to your operating system. 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:
Open the webpage of the Instagram account. Click on the browser Menu > More Tools > Developer Tools or use the shortcut Ctrl + Shift + I to activate the Developer Tools view. 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. 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. 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.
Import the required modules into the Python environment. from selenium import webdriverimport timeimport urllib. request 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. #entering the username of the profile whose profile picture is to be downloaded username=input(“Enter the username of the profile: “) Initialize the web driver by creating an object of it and passing its file system path. #creating an object of chromedriver cd=‘chromedriver. exe’ Use the webdriver. Chrome function to launch the Google Chrome browser. #open google chrome browser driver = webdriver. Chrome(cd) 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, #setting the urlurl=‘https://www. instagram. com/’ url_p=url+user_h Pass the complete URL of the Instagram profile to be visited to the get() function. #open the profile driver. get(url_p) Set an optional recommended delay for the web page to load completely. #delay for page content loadingtime. sleep(5) 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:#if profile is public, search for image with class _aa8jimage=driver. find_element_by_xpath(’//img[@class="_aa8j”]’)except:#if profile is private, search for image with class _aadpimage=driver. find_element_by_xpath(’//img[@class="_aadp”]’) Using the get_attribute(), gain the src attribute of the image. This returns the link of the image. #store the download link of image img_link=image. get_attribute(‘src’) 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. #set path of downloaded file path=“D:\"+username+”. jpg" 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. #downloading image to the required destinationurllib. request. urlretrieve(img_link,path) 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. #displaying the path of the profile pic 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:
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.