Thought leadership from the most innovative tech companies, all in one place.

A Free Instagram Bot for Growing Your Follower Base

The bot will follow people like the posts and comment as well

Selenium bot logging in on Instagram — Image by authorSelenium bot logging in on Instagram — Image by author

Note: This bot was created purely for educational purposes. Bot creation on platforms such as Instagram is considered a violation of their terms and conditions. Doing so may result in your account being banned, and possible legal action taken against you.

I have been looking for a simple automation script/source code which I can just copy and use to automate my instagram account. The primary purpose of automation was learning but I also wanted to increase my following on Insta. Sure, there are online services that are selling Instagram followers and can give you 1000 to 5000 followers in a day, but soon I learned that most of these followers are useless because (i) When you have done the payment the follower count drops a lot later on (ii) Most of those followers are just bots and fake accounts so there is not much boost in engagement

So instead of buying followers, the automation is a far better option which will increase your follower base organically or technically semi-organically without spending a penny. I even found some automation scripts. But either they were paid, and the ones free were outdated or just performed basic steps like opening the browser or logging in on Instagram. So I decided to write a simplest script myself which will also like, follow and comment on the posts which will increase our visibility hence follower base and make easier for our target audience to find us.

Things to Remember About Instagram Automation

Instagram UI changes a lot a small change in the position of a button may cause your script to fail (that is the reason that only a couple months old script were failing to locate elements as well)

I have given some configuration variables in the script which you can alter to speed up the growth of your account, but that also makes your account vulnerable to be marked as spam. As I have seen people try to get 500 followers a day from new account. People get notifications and eventually get banned for spamming as well. So just keep it as slow as possible. Following 30 to 50 people a day is not a bad idea for a fresh account.

Plus Points of the Script

It is built on Python, one of the easiest programming languages. So you can understand this article pretty easily as well as run this script on your laptop and use your own instagram account

  • I have added the extensive comments in the script, so it will help you understand the code as well

  • I have used print commands extensively so you know which part of your code is executing

  • It pauses for few seconds after every action, but interesting part is that it waits for random number seconds and not just hardcoded number of seconds as seen in other scripts

Workflow of the Script

Here is the high level overview of what the script does:

  1. Opens the browser

  2. Goes to instagram.com

  3. Tries to login using credentials you added in .env file

  4. Go to the first hashtag mentioned in the script

  5. Selects the first post of that hashtag

  6. If already liked moves to the next picture otherwise, proceeds to the next steps

  7. Likes the picture. Follows the user/author of the post, if not following already.

  8. Optionally comment based on the set probability. The bot will choose comment randomly as set in the commentsList (the more comments you add the better)

  9. Moves to the next post, and repeat actions 7 and 8 till the number of posts mentioned

  10. Moves to the next hashtag and repeat actions 5 to 9 until all hashtags are done

  11. Script completed. Prints a small report showing the number of follows, likes and comments

Before You Get Started:

Optionally, you have to set these variables in the script:

  • Install pip, it is used for downloading further python packages/libraries easily (you can install pip alongwith Python)

  • Once you have pip, you need to install required libraries. Open command prompt or terminal depending on your OS, and run the following command

    pip install selenium python-dotenv

  • Install either Geckodriver (if you want to run on Firefox) or Chrome driver (if you want to run on Google Chrome)from the drivers list here. Here, we are using Firefox

  • Change the configuration variables of hashtag list (usually relevant hashtags, the ones that your competitors are using), generic comments list from which the bot will choose one for every post randomly (the more comments the better), number of posts you want to engage, and probability of commenting on a post in the configuration section as shown below

    from selenium import webdriver from selenium.webdriver.common.keys import Keys from time import sleep, strftime from random import randint from selenium.webdriver.common.by import By import os from dotenv import load_dotenv load_dotenv() import datetime

    ########## Change the below configuration variables

    hashtag_list = [ 'dailyquotes', 'quoteoftheday'] # 'deepthoughts', 'dailyinspiration', 'inpired', 'selfgrowth', 'motivationalthoughts', 'bepositive', 'dailymotivation', 'personalgrowth'] # Change this to your own tags

    commentsList = ['Really cool!', 'Nice work ❤️', '👏👏', 'wow 👏', 'well said', 'so cool! 🙌', '❤️', 'lovely 😍', 'amazing 👌', 'amazing', 'wow ❤️', 'well said 👏', '👏👏👏👏', 'Nice!', 'nice 🔥', 'feels good 😍', 'Good job! 😊', '❤️🔥🔥😍', 'thoughtful 👌', 'thatz deep', 'intense 💯', '😊😊', 'i will take it as quote of the day','too much motivation 👌','very inspiring ❤️🔥']

    total_posts = 5 # per hashtag

    comment_percentage = 70

    ########## Change above configuration variables

Running The Script

Now you just need two files. instabot.py (the main script) and .env file (where you enter your username and password), you can get both these files from this Github repo. ows-ali/instabot *An Instagram automation bot using Selenium. The bot can login, like, follow and comment on the posts Install pip, it is…*github.com

Here is the main portion of source code:

for hashtag in hashtag_list:

  print('===>On hashtag: ', hashtag)

  tag += 1

  webdriver.get('https://www.instagram.com/explore/tags/'+ hashtag_list[tag] + '/')

  sleep(randint(2,4))

  first_thumbnail = webdriver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[1]/a/div')
print("===>Clicking first post of the hashtag",hashtag)

  first_thumbnail.click()

  sleep(randint(1,2))

try:

  for x in range(1,total_posts+1):

  print('Hashtag: ', hashtag, 'Post: ', x)

  # If we already like, then do nothing with the post, go to next post

  alreadyLike = webdriver.find_elements_by_xpath( "//section/span/button/div/span[*[local-name()='svg']/@aria-label='Like']")

  if len(alreadyLike) == 1:

    print('===>Following the user if we have follow button')

    button_follow = webdriver.find_element_by_xpath("//button[text()='Follow']")

    if button_follow.text == 'Follow' :

      print("===following user now in few seconds")

      sleep(randint(4,6))

      button_follow.click()

      followed += 1

    else:

      print("===>No follow button available, so skipping the following")

    # Liking the picture

    print('===>Liking the picture')

    sleep(randint(1,2))

    button_like = webdriver.find_element_by_xpath('/html/body/div[5]/div[2]/div/article/div[3]/section[1]/span[1]/button')

    button_like.click()

    likes += 1


   # Comments and tracker

    comm_prob = randint(1,10)

    print('===>Trying to comment if probability is found to be more than',comment_percentage)

    print('===>Probability found to be: ', comm_prob*10)

    if comm_prob > comment_percentage/10:

      waitTime = randint(1,8)

      print('===>So commenting')

      sleep(randint(1,8))
      webdriver.find_element_by_xpath( '/html/body/div[5]/div[2]/div/article/div[3]/section[1]/span[2]/button').click()

      sleep(randint(2,4))

      comment_box = webdriver.find_element_by_xpath('/html/body/div[5]/div[2]/div/article/div[3]/section[3]/div/form/textarea')

      commentIndex = randint(0,len(commentsList)-1)

      print('===>printing comment number {0}, which is {1}'.format(commentIndex+1, commentsList[commentIndex]))

      comment_box.send_keys(commentsList[commentIndex])

      sleep(1)


      # Enter to post comment

      comment_box.send_keys(Keys.ENTER)

      comments += 1

      print('===>Commented! now waiting few seconds')

      sleep(randint(3,6))

    else:

      print('===>Skipping commenting, since probability is low')

Future Improvements

Here are the few things that I will improve upon, and if you want you can contribute as well by raising a pull request on Github repo.

  • Refactor and restructure the code

  • Add feature to unfollow people that we followed, in chronological order

  • Based on a given list of usernames (like that of celebrities or competitors), follow their followers and like their recent posts

  • Whenever a competitor post something on insta, like their post and add comments

  • Whenever the script raises exception, an email should be sent to the admin immediately with details of the exception

Conclusion

We have built an Instagram bot that is surely going to give you a followers boost. The usual follow back ratio on my account is one-fifth or one-fourth but it can be more or less depending upon how good your instagram profile is and how relevant audience you targeted. You can set the configuration variables to improve the growth, but just be cautious, don’t be greedy. You should increase the flux after a week or more. Stay tuned if you want more such scripts for automating your social media accounts.

Coming up next, automation of:

  • Twitter

  • Facebook




Continue Learning