#FollowForFollow — Build an Instagram Bot to Gain Followers

Katherine Hanks
5 min readApr 5, 2021
Medium said to post a picture so here is my dog Betty. She loves Selenium and Instagram as much as I do.

Let’s face it, whether you are a small-business owner, a celebrity, or a regular ole’ millennial — we all want more followers on Instagram. Growing your Instagram means more opportunities for exposure and is the difference between posting about a vitamin company you really like and getting paid to post about a vitamin company you really like. The 1,000 True Fans rule states that any business only needs 1,000 dedicated purchasers (True Fans) to make a living. But if you are just starting out, how do you get to this magical number?

That’s where I come in.

Do you remember the saying “It takes money to make money?” Well, it takes following to build your own following, and if you build it they will come! (I know, too many adages ¯\_(ツ)_/¯)

During the pandemic I, like many, found myself to be bored and in need of a project (and something to distract me from my graduate school work). I was learning Python in school and using scrapers at my day job when the light bulb went off — Why don’t I create an Instagram scraper to gain followers and become the IG influencer I have always wanted to be?! I was obsessed with the Selenium package and got to work on researching and testing its capabilities.

Now a few warnings here before we get started. This type of “bot” is gray area for IG’s terms and conditions. I have built it so that it doesn’t go through and follow too many people at one time so that you do not get shadow banned, but USE AT YOUR OWN RISK. Additionally I don’t recommend using this program every day, maybe every other day or every three days. After I perfected the code, I was able to use this program for a restaurant that had its old Instagram hacked and grow their new following from 74 followers to 1,275 followers, and they are currently following 4,022 profiles (I bartered this program for free food — so keep this in mind, folks).

Let’s begin by importing our libraries. We ❤ Selenium and this example requires Chrome, but the library does have FireFox and Edge functions.

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from time import sleep, strftime
from random import randint
import pandas as pd

Next, we declare our Chrome options. When I first ran this program, I didn’t do anything with options but one day my Chrome just started crashing over and over again so the — no sandbox option was suggested by the Holy Bible of Stack Overflow to stop the problem.

Now we go and log into Instagram!

Note: From this point sleep will be our best friend because this function is what stops us from looking too botty, if you will.

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no sandbox')
#options=chrome_options
webdriver = webdriver.Chrome(ChromeDriverManager().install())
sleep(2)
webdriver.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
sleep(3)

Put in your username and password to sign in, then we sleep.

username = webdriver.find_element_by_name('username')
username.send_keys('katiehanksxo')
password = webdriver.find_element_by_name('password')
password.send_keys('Follow me don't steal my password!')
button_login = webdriver.find_element_by_css_selector('#loginForm > div > div:nth-child(3) > button')
button_login.click()
sleep(3)

After logging in, sometimes Instagram gives pop-ups like “Do you want us to remember your login” or “do you want to turn on notifications” so we need to skip those by pressing the “Not Now” button. If you do not receive either of these pop-ups you can comment out these lines. Also, from time to time Instagram changes up the CSS/xpath so you will need to become CSS/xpath savvy to keep this code up to date. If xpath doesn’t work for you, try CSS and vice versa.

try:
not_now = webdriver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/div/div/div/button')
not_now.click()
except NoSuchElementException:
webdriver.close()
print('Fail, Try Again')
try:
really_not_now = webdriver.find_element_by_css_selector('body > div.RnEpo.Yx5HN > div > div > div > div.mt3GC > button.aOOlW.HoLwm')
really_not_now.click()
except NoSuchElementException:
webdriver.close()
print('Fail, Try Again')

Using our restaurant example, next I chose two hashtags that I thought had relevance for the restaurant: foodies in South Florida. Before continuing, this part is extremely important if you want to gain a meaningful following. You can try the top hashtags like #love or #animals, but will those hashtags advance your personal brand or business? Most likely not. I ran this program for an advertising agency once and we used hashtags like #southfloridarealtor or #southfloridarealestate because they knew realtors were most likely to buy ads.

hashtag_list = ['miamifoodies','miamifood']
#Keep track of how many folks you follow
new_followed = []
tag = -1
followed = 0
likes = 0
comments = 0

Next, the program uses the hashtag to explore instagram. Usually it will show you the Top 10 photos for that hashtag then move on to more recent ones, mimicking real-time engagement. It sleeps in between following profiles, so I recommend hitting the play button on Pycharm and stepping away for a few hours. Also if the program comes to a profile you already follow, it will skip it and move on.

I have almost forgotten my FAVORITE part of this program…it will randomly comment on pictures for you. I found this portion on another Instagram Bot tutorial that I have previously cited, and I absolutely loved it. One of the main things I know about Instagram is that your account will show up the more you engage with others’ content, so this step is absolutely necessary. But, please update the comments to be something that is more on brand for you and not early 2000’s Paris Hilton quotes.

for hashtag in hashtag_list:
tag += 1
webdriver.get('https://www.instagram.com/explore/tags/' + hashtag_list[tag] + '/')
sleep(5)
first_thumbnail = webdriver.find_element_by_xpath(
'//*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[1]/a/div')

first_thumbnail.click()
sleep(randint(1, 2))

for x in range(1, 400):
username = webdriver.find_element_by_css_selector(
'body > div._2dDPU.CkGkG > div.zZYga > div > article > header > div.o-MQd > div.PQo_0 > div.e1e1d > span > a').text

if username not in prev_user_list:
# If we already follow, do not unfollow
if webdriver.find_element_by_css_selector(
'body > div._2dDPU.CkGkG > div.zZYga > div > article > header > div.o-MQd > div.PQo_0 > div.bY2yH > button').text == 'Follow':

webdriver.find_element_by_css_selector(
'body > div._2dDPU.CkGkG > div.zZYga > div > article > header > div.o-MQd > div.PQo_0 > div.bY2yH > button').click()

new_followed.append(username)
followed += 1

# Liking the picture
button_like = webdriver.find_element_by_css_selector(
'body > div._2dDPU.CkGkG > div.zZYga > div > article > div.eo2As > section.ltpMr.Slqrh > span.fr66n > button')

button_like.click()
likes += 1
sleep(randint(18, 25))

# Comments and tracker
comm_prob = randint(1, 10)
print('{}_{}: {}'.format(hashtag, x, comm_prob))
if comm_prob > 7:
comments += 1
webdriver.find_element_by_css_selector(
'body > div._2dDPU.CkGkG > div.zZYga > div > article > div.eo2As > section.ltpMr.Slqrh > span._15y0l > button').click()
comment_box = webdriver.find_element_by_css_selector(
'body > div._2dDPU.CkGkG > div.zZYga > div > article > div.eo2As > section.sH9wk._JgwE > div > form > textarea')

if (comm_prob < 7):
comment_box.send_keys('loves it!')
sleep(1)
elif (comm_prob > 6) and (comm_prob < 9):
comment_box.send_keys('thats hot')
sleep(1)
elif comm_prob == 9:
comment_box.send_keys('sanassa!')
sleep(1)
elif comm_prob == 10:
comment_box.send_keys('loves it')
sleep(1)
# Enter to post comment
comment_box.send_keys(Keys.ENTER)
sleep(randint(22, 28))
else:
print('Error with Follow Button')

# Next picture
webdriver.find_element_by_link_text('Next').click()
sleep(randint(23, 28))

else:
print('Error with Username')
webdriver.find_element_by_link_text('Next').click()
sleep(randint(21, 27))

print('Liked {} photos.'.format(likes))
print('Commented {} photos.'.format(comments))
print('Followed {} new people.'.format(followed))

webdriver.close()

And voilà! Press play and step away.

That’s it, that’s the code. Big thanks to this article that ultimately helped me build the base of the program.

Repository here!

Still lost? DM me on IG: @katiehanksxo and I can help you set it up!

--

--

Katherine Hanks

IG: @katiehanksxo I love: Data, Dogs, and Simple Life quotes.