Not so long ago, I saw some websites which were offering Instagram likes and followers, some of them were paid and some were free. After some research, I realized how some users basically crawl for followers. They comment, like and follow people.

After seeing these websites, I was curious how these websites even work.

I can’t say seeing these websites and apps was a surprise to me, that there were bots in Instagram, it just made me want to build one myself

I came up with a simple bot in Python, while I was just playing and messing around with Selenium.

Talking of Selenium, simply put, Selenium is like a browser you can interact with very easily in Python.

What the heck is Selenium?

So, I made what world needed the most, Another Instagram bot 😁

So, what does this bot do?

Well basically it likes other people’s Instagram photos in different hashtags. In return some people like back my photos and if they like my gallery some even follow me. Like for like you can say.

To be clear, I’m not using this bot intensively, as Instagram will stop responding if you run it too fast, and if the bot doesn’t pause in every like Instagram may ban my account for short period of time. To tackle this problem, I used some sleep commands after every like and next click.

You can create your own bot with follow and comment functionality for better growth because people are less inclined to follow based on a single like or comment, I’ll make a post on this as well later.

Steps to make the bot

You need a code editor, I used sublime text but you can use any code editor.

Since we are using Python, we need to install these packages in order to make the bot.

  • Selenium
  • Panda

Run these commands one by one in terminal/cmd to install these packages.

python -m pip install selenium
python -m pip install panda

We also need Chrome Web Driver, download from here and extract it.

Then create a new python file name it whatever you want, I named it instabot.py

Code

You can find my bot in this GitHub repository

or you can use below code, I’ll explain every step later. Copy & paste it in your python file

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep, strftime
from random import randint
import pandas as pd
import logging
import sys

# Change this to your own chromedriver path!
chromedriver_path = 'C:/Users/Suda/Downloads/Compressed/chromedriver_win32_2/chromedriver.exe'
webdriver = webdriver.Chrome(executable_path=chromedriver_path)
sleep(2)
webdriver.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
sleep(3)

username = webdriver.find_element_by_name('username')
# Change with your username
username.send_keys('USER_NAME')
password = webdriver.find_element_by_name('password')
# Change with your password
password.send_keys('PASS_WORD')
try:
    button_login = webdriver.find_element_by_css_selector('#react-root > section > main > div > article > div > div:nth-child(1) > div > form > div:nth-child(4) > button')
except:
    button_login = webdriver.find_element_by_css_selector('#react-root > section > main > article > div.rgFsT > div:nth-child(1) > div > form > div:nth-child(4) > button')
button_login.click()
sleep(5)
try:
    savebt = webdriver.find_element_by_css_selector('#react-root > section > main > div > div > div > div > button')
    savebt.click()
except:
    pass

try:
    notnow = webdriver.find_element_by_css_selector('body > div.RnEpo.Yx5HN > div > div > div > div.mt3GC > button.aOOlW.HoLwm')
    notnow.click()
except:
	pass

# Change with hashtags you want to use
hashtag_list = ['lightroom', 'mobilephotography', 'like4like']

prev_user_list = []

d_users = []
tag = -1
likes = 0

for hashtag in hashtag_list:
    tag += 1
    # Opens hashtag
    webdriver.get('https://www.instagram.com/explore/tags/'+ hashtag_list[tag] + '/')
    sleep(5)
    # It clicks on first photo to open photo window
    first_thumbnail = webdriver.find_element_by_css_selector('#react-root > section > main > article > div.EZdmt > div > div > div:nth-child(1) > div:nth-child(1) > a > div')
    first_thumbnail.click()
    sleep(randint(1,2))    
    
    try:        
        for x in range(1,200):
            # Copies username of user
            username = webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[1]/div/a').text
            print(username)
            d_users.append(username)

            # Clicks on like button
            button_like = webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[3]/section[1]/span[1]/button')
            button_like.click()

            # Increaments likes array
            likes += 1
            sleep(5)
          
            # Next picture
            webdriver.find_element_by_link_text('Next').click()
            sleep(randint(25,29))
    except:
        # If error occurres, skip to next hashtag
        print("Oops!", sys.exc_info()[0], "occurred.")
        continue

for n in range(0,len(d_users)):
    prev_user_list.append(d_users[n])
    
updated_user_df = pd.DataFrame(prev_user_list)
updated_user_df.to_csv('{}_users_list.csv'.format(strftime("%Y%m%d-%H%M%S")))
print('Liked {} photos.'.format(likes))

Explanation

First of all, we import our desired packages in our python bot, this step is important in order to make the bot.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep, strftime
from random import randint
import pandas as pd
import logging
import sys

After importing packages, we declare our Chrome web driver path in chromedriver_path, so we can use selenium with chrome

# Change this to your own chromedriver path!
chromedriver_path = 'C:/Users/Suda/Downloads/Compressed/chromedriver_win32_2/chromedriver.exe'
webdriver = webdriver.Chrome(executable_path=chromedriver_path)
sleep(2)

After declaring web driver, we open Instagram login page to login with our account

webdriver.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
sleep(3)

When page completely loads, bot puts our login username and password in thier fields and click on login button. Just change USER_NAME and PASS_WORD with your username and password.

username = webdriver.find_element_by_name('username')
# Change with your username
username.send_keys('USER_NAME')
password = webdriver.find_element_by_name('password')
# Change with your password
password.send_keys('PASS_WORD')
try:
    button_login = webdriver.find_element_by_css_selector('#react-root > section > main > div > article > div > div:nth-child(1) > div > form > div:nth-child(4) > button')
except:
    button_login = webdriver.find_element_by_css_selector('#react-root > section > main > article > div.rgFsT > div:nth-child(1) > div > form > div:nth-child(4) > button')
button_login.click()
sleep(5)

Sometimes Instagram offers you to save your credentials and enable notifications, which we don’t want to enable in our bot. So, we cancel all popups using below code.

try:
    savebt = webdriver.find_element_by_css_selector('#react-root > section > main > div > div > div > div > button')
    savebt.click()
except:
    pass

try:
    notnow = webdriver.find_element_by_css_selector('body > div.RnEpo.Yx5HN > div > div > div > div.mt3GC > button.aOOlW.HoLwm')
    notnow.click()
except:
	pass

Now we define some variables

# Change with hashtags you want to use
hashtag_list = ['lightroom', 'mobilephotography', 'like4like']

prev_user_list = []

d_users = []
tag = -1
likes = 0

Now we want our bot to loop through all hashtags we provided earlier. In this loop we create another loop to loop through photos of hashtags (I am using 200 photos per hashtag; you can change it with your desired number). And in this that nested loop we want our bot to like every photo that comes under this loop, we have to provide proper XPaths and Selectors for username element and like button element. We are also saving username of every person’s photo that bot likes.

for hashtag in hashtag_list:
    tag += 1
    # Opens hashtag
    webdriver.get('https://www.instagram.com/explore/tags/'+ hashtag_list[tag] + '/')
    sleep(5)
    # It clicks on first photo to open photo window
    first_thumbnail = webdriver.find_element_by_css_selector('#react-root > section > main > article > div.EZdmt > div > div > div:nth-child(1) > div:nth-child(1) > a > div')
    first_thumbnail.click()
    sleep(randint(1,2))    
    
    try:        
        for x in range(1,200):
            # Copies username of user
            username = webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[1]/div/a').text
            print(username)
            d_users.append(username)

            # Clicks on like button
            button_like = webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[3]/section[1]/span[1]/button')
            button_like.click()

            # Increaments likes array
            likes += 1
            sleep(5)
          
            # Next picture
            webdriver.find_element_by_link_text('Next').click()
            sleep(randint(25,29))
    except:
        # If error occurres, skip to next hashtag
        print("Oops!", sys.exc_info()[0], "occurred.")
        continue

After bot gets done with liking we want it to create and store all username in a csv file

for n in range(0,len(d_users)):
    prev_user_list.append(d_users[n])
    
updated_user_df = pd.DataFrame(prev_user_list)
updated_user_df.to_csv('{}_users_list.csv'.format(strftime("%Y%m%d-%H%M%S")))
print('Liked {} photos.'.format(likes))

Technical issue

The code is really simple, sometimes Instagram change the class’s names in that case we also have to update our XPaths and Selectors, which we can do by opening Inspect tab in chrome and selecting that element and right clicking and choosing copy->XPath/Selector.

And that’s it!

After few hours of running this bot you may get some likes in your latest photos. You can stretch it even further by implementing follow and comment function.

Thanks for reading! If you liked this article, please share it with your friends and Instagram fans 😄

If you’d like to get in touch, you can DM me on Instagram at @ankit_suda or you can simply comment below.

Photo by Lisa Fotios from Pexels

Leave a Reply