DEV Community

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

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

chitturiarunkrishna profile image Arun krishna Updated on ・3 min read

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

Discussion (1)

pic
Editor guide

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