DEV Community

Cover image for Instagram bot to like and comment on all the posts of an user
Arun krishna
Arun krishna

Posted on • Edited on

9 2

Instagram bot to like and comment on all the posts of an user

Disclaimer:
This code is for educational purposes only and the author is not responsible for any consequences resulted. Please do not try it if you do not agree for the condition.

Hello everyone,
In this post I will be showing how I automated instagram to like and comment on all the posts of a particular user

Prerequisites:
*Selenium
*Python
*Webdriver

I am using Google chrome web driver to automate our work, even Gecko driver which is Mozilla firefox webdriver. First install the webdriver in your workspace and import selenium in the workspace.

import selenium

Next we need to import keys and webdriver from selenium. Keys are used to send our text into the input fields

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

Next we need to find out the xpath of the elements from the site in order to send our parameters. We can either call the required element by it's class name or by using the xpath.

Xpath
Xpath can be extracted by clicking on inspect element and then by clicking on Copy full xpath option. After getting the xpath we use driver.find_element_by_xpath command.

For clicking on any Button or Link we use click() function.

We use driver.get command to open Instagram. And then we open our required user profile by using
driver.get("https://www.instagram.com/"+OtherUserId)

Now we get the number of posts of that particular user

posts = self.driver.find_element_by_xpath("/html/body/div/section/main/div/header/section/ul/li/span/span").text
posts = Decimal(sub(r'[^\d.]', '', posts))

Like

Next we open the individual posts by using the classname driver.find_element_by_class_name("_9AhH0"). Next we click on the like button

like = self.driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[3]/section[1]/span[1]/button')
like.click()

Next similarly, for Comment we pull the xpath of the comment section and then send the text we need to comment and then comment is posted.

cmt = self.driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[3]/section[1]/span[2]/button')
cmt.click() self.driver.find_element_by_xpath("/html/body/div[4]/div[2]/div/article/div[3]/section[3]/div/form/textarea").send_keys(comment)    self.driver.find_element_by_xpath("/html/body/div[4]/div[2]/div/article/div[3]/section[3]/div/form/textarea").send_keys(Keys.ENTER)

Similarily we need to iterate it for all the posts.

Python code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from re import sub
from decimal import Decimal

class Instabot:
    def __init__(self,username,password,OtherUserId,comment):
        self.driver=webdriver.Chrome("chromedriver.exe")
        self.driver.maximize_window()
        self.driver.get("https://www.instagram.com/")
        sleep(2)
        self.driver.find_element_by_xpath("//input[@name=\"username\"]").send_keys(username)
        self.driver.find_element_by_xpath("//input[@name=\"password\"]").send_keys(password)
        self.driver.find_element_by_xpath("//button[@type=\"submit\"]").click()
        sleep(5)
        self.driver.find_element_by_xpath("//button[text()='Not Now']").click()
        sleep(5)
        self.driver.find_element_by_xpath("//button[text()='Not Now']").click()
        sleep(5)
        self.driver.get("https://www.instagram.com/"+OtherUserId)
        posts = self.driver.find_element_by_xpath("/html/body/div/section/main/div/header/section/ul/li/span/span").text
        posts = Decimal(sub(r'[^\d.]', '', posts))
        print(posts)
        pic = self.driver.find_element_by_class_name("_9AhH0")    
        pic.click()
        sleep(2)
        like = self.driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[3]/section[1]/span[1]/button')
        like.click()
        cmt = self.driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[3]/section[1]/span[2]/button')
        cmt.click()
        sleep(1)
        self.driver.find_element_by_xpath("/html/body/div[4]/div[2]/div/article/div[3]/section[3]/div/form/textarea").send_keys(comment)
        self.driver.find_element_by_xpath("/html/body/div[4]/div[2]/div/article/div[3]/section[3]/div/form/textarea").send_keys(Keys.ENTER)
        nextPic = self.driver.find_element_by_xpath('/html/body/div[4]/div[1]/div/div/a')
        nextPic.click()
        print("success") 
        sleep(2)
        for i in range(int(posts-1)):
            like = self.driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[3]/section[1]/span[1]/button')
            sleep(2)
            like.click()
            sleep(2)
            cmt = self.driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[3]/section[1]/span[2]/button')
            cmt.click()
            sleep(1)
            self.driver.find_element_by_xpath("/html/body/div[4]/div[2]/div/article/div[3]/section[3]/div/form/textarea").send_keys(comment)
            self.driver.find_element_by_xpath("/html/body/div[4]/div[2]/div/article/div[3]/section[3]/div/form/textarea").send_keys(Keys.ENTER)
            nextPic = self.driver.find_element_by_xpath('/html/body/div[4]/div[1]/div/div/a')
            nextPic.click()
            print("success") 
            sleep(2)
    sleep(20)
Instabot('yourusername','yourpassword','otheruser','your comment')

Here is the entire repository. Please feel free to drop a star

GitHub logo Chitturiarunkrishna / Instalikeandcmtbot

Explanation is available in this link

Instagram bot to like and comment on all the posts of an user

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (2)

Collapse
 
quincybrooks profile image
Christopher

Great post! Your guide on creating an Instagram bot to like and comment on posts is really helpful. Does this method work well with Instagram’s latest updates, or do you recommend any changes? Also, for those looking to grow their accounts, a Insta up APK like InstaUP could be a useful tool alongside automation.

Collapse
 
abdisalan_js profile image
Abdisalan
Comment hidden by post author

Some comments have been hidden by the post's author - find out more

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay