Like instagram pictures using Selenium and Python


With over a billion members, Instagram is a hugely successful social networking website. It is a favourite platform for data scientists, marketers, and developers alike because to its strong API and data accessibility. With the aid of 2-3 examples, we will examine how to like Instagram photos programmatically using Python and Selenium in this article.

Please be aware that this information is only meant to be instructive. Respecting Instagram's rules and refraining from behaviour that can be construed as spamming are crucial.

Getting Started: Installation and Setup

You need to set up your environment before you start coding. Python and Selenium are needed. Python may be obtained from the program's official website, and pip can be used to set up Selenium:

pip install selenium

Additionally, download the most recent version of ChromeDriver, a necessary tool for Selenium to communicate with Chrome.

Using Selenium for Instagram

Selenium is an effective tool for remotely manipulating a web browser. It works well for actions like clicking and typing, which we will use to like Instagram photographs, which call for direct contact with the browser.

Utilising Python and Selenium to Like Instagram Images

A step-by-step tutorial for writing a Python script to automatically like Instagram photos is provided below −

Step 1: Importing Required Libraries

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

Step 2: Access Instagram

We have to go to the Instagram website and sign in using your information. With Selenium, we can automate this procedure 

driver = webdriver.Chrome('path_to_your_chromedriver')

driver.get('https://www.instagram.com/')

# Pause for 3 seconds to let the page load
time.sleep(3)

# Locate the username and password fields and input your credentials
username = driver.find_element_by_name('username')
username.send_keys('your_username')

password = driver.find_element_by_name('password')
password.send_keys('your_password')

# Click on the login button
password.send_keys(Keys.RETURN)

Step 3: Navigate to the Desired Page

To like their images, let's now go to a specific Instagram page. 'instagram_page' should be replaced with the appropriate page's username 

# Pause for 3 seconds to let the page load
time.sleep(3)

# Navigate to the specific Instagram page
driver.get('https://www.instagram.com/instagram_page')

Step 4: Liking Pictures

We'll choose every image on the page first, and then like them all −

# Pause for 3 seconds to let the page load
time.sleep(3)

# Find all the picture elements on the page
pictures = driver.find_elements_by_class_name('_9AhH0')

for picture in pictures:
   # Click on each picture
   picture.click()

   # Pause for 3 seconds to let the picture load
   time.sleep(3)

   # Click on the like button
   like_button = driver.find_element_by_class_name('fr66n')
   like_button.click()

   # Close the picture
   driver.find_element_by_class_name('Igw0E').click()

   # Pause for 3 seconds before moving to the next picture
   time.sleep(3)

Couple of Examples

1. Liking Pictures by Hashtag

In the last illustration, we went to a certain user's page. Let's now visit a page of a specific hashtag and "like" the images there 

# Navigate to the specific Instagram hashtag page
driver.get('https://www.instagram.com/explore/tags/hashtag')

The picture-liking procedure is then carried out as before:

# Pause for 3 seconds to let the page load
time.sleep(3)

# Find all the picture elements on the page
pictures = driver.find_elements_by_class_name('_9AhH0')

for picture in pictures:
   # Click on each picture
   picture.click()

   # Pause for 3 seconds to let the picture load
   time.sleep(3)

   # Click on the like button
   like_button = driver.find_element_by_class_name('fr66n')
   like_button.click()

   # Close the picture
   driver.find_element_by_class_name('Igw0E').click()

   # Pause for 3 seconds before moving to the next picture
   time.sleep(3)

2. Liking Only a Certain Number of Pictures

To avoid appearing spammy, you might wish to restrict the quantity of images you post in some circumstances. The script can be changed to only like the first 'n' photographs on a page as seen below 

# Define the number of pictures to like
num_pictures_to_like = 5

# Find all the picture elements on the page
pictures = driver.find_elements_by_class_name('_9AhH0')[:num_pictures_to_like]

for picture in pictures:
   # Click on each picture
   picture.click()

   # Pause for 3 seconds to let the picture load
   time.sleep(3)

   # Click on the like button
   like_button = driver.find_element_by_class_name('fr66n')
   like_button.click()

   # Close the picture
   driver.find_element_by_class_name('Igw0E').click()

   # Pause for 3 seconds before moving to the next picture
   time.sleep(3)

The number of images we can choose from is constrained in this script by only returning the top 'n' elements from the collection of photographs.

Conclusion

Various situations, such as data analysis and digital marketing, might greatly benefit from automating Instagram interactions. However, in order to respect user privacy and Instagram's usage guidelines, it's crucial to use these tools appropriately.

Selenium and Python together offer a potent toolkit for web automation. The examples used to illustrate this manual only scratch the surface of what these technologies are capable of.

Updated on: 18-Jul-2023

430 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements