DEV Community

Cover image for Instagram Bot
Sunil Aleti
Sunil Aleti

Posted on • Updated on

Instagram Bot

GitHub logo aletisunil / Instagram-Bot

Automate Instagram


Hey Geeks,
Hope everyone are safe in this pandemic situation and spending time wisely, even I'm bored so i automated Instagram to like posts.
Today i will be sharing a tutorial on how I automated Instagram to like posts of a respective user.
While jumping in to tutorial,

Prerequisites:

Intially, we need selenium to automate, And we can check by executing those commands in your python IDLE or Code Editors

import selenium
print selenium.__version__
Enter fullscreen mode Exit fullscreen mode

Next, we need to install webdriver and you can choose either Google chrome or mozilla firefox webdriver and I gave a link to install Chrome webdriver.

And now let's dig in to the code
Step 1:
we need to import webdriver and keys from selenium, these Keys are used to send username and password in input fields.
import sleep from time

Step 2:
Create a class which takes username,password and Other user id to whom you want to like the posts
And next you need to locate webdriver and assign to a variable, in my case my webdriver is located in "/Users/Aleti Sunil/Downloads/chromedriver_win32/chromedriver.exe"

Step 3:
In order to open Instagram, we need to use driver.get command
And for entering username and password fields, we need to locate the input field by using XPATH, driver.find_element_by_xpath command
To click on button or any link, we use click() function.

Step 4:
After successfull login, we need to drive in to the person's profile to whom we want to like the posts and that can be done by driver.get("https://www.instagram.com/"+OtherUserId)
After going in to user profile, we need to get number of posts so that we can iterate over the posts.

Step 5:
we need to pull latest post from the his/her profile by using classname or xpath, I used classname to pull the post
driver.find_element_by_class_name("_9AhH0")
To like the posts, we need to get the xpath of that like button
driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[1]/button')
And to iterate over other posts we need find xpath for "next" button, so that this bot can iterate till last post.

Step 6:
You need to enter ur username, password and username of other person to whom you want to like
Ex:
Instabot('abcxcwe','dfasd32963','capdt')#username,password,username of other person
I added sleep commands so that page gets loaded by the time otherwise an error is thrown.

And here is python code and video demo

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):
        self.driver=webdriver.Chrome("/Users/Aleti Sunil/Downloads/chromedriver_win32/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(10)
        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[2]/section[1]/span[1]/button')
        like.click()
        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[2]/section[1]/span[1]/button')
            sleep(2)
            like.click()
            sleep(2)
            nextPic = self.driver.find_element_by_xpath('/html/body/div[4]/div[1]/div/div/a[2]')
            nextPic.click() 
            sleep(2)
    sleep(20)
Instabot('abcxcwe','dfasd32963','capdt') #Username,password,Username of other person
Enter fullscreen mode Exit fullscreen mode

P.S: If post is already liked, it's going to be disliked and you need to have atleast some basic knowledge of XPATH, So that you can have clear cut view of the code.

If you have any queries, please let me know.

Top comments (7)

Collapse
 
redboxjm1 profile image
redboxjm1

How would you go about automating comments in this project?

Collapse
 
sunilaleti profile image
Sunil Aleti

Yeah we can but these bots violate Instagram rules 😓

Collapse
 
redboxjm1 profile image
redboxjm1

Yeah. I'm trying to create a signup bot, but having some problems referencing the drop down list on the second page of sign for the dates,

any help you can provide would be appreciated.

thanks

Collapse
 
daxdax89 profile image
DaX

I am assuming your test account got banned after one day?

Thread Thread
 
sunilaleti profile image
Sunil Aleti

nope btw that's my personal account

Thread Thread
 
daxdax89 profile image
DaX

Which timing/speed did you use? Was it worth using the bot btw? Did it really help you grow some audience?

Thread Thread
 
sunilaleti profile image
Sunil Aleti

for every operation I set an sleep(1) and for the sake of tutorial I made this .. not for gaining audience 🙂