Open In App

Make an Instagram Bot With Python

Last Updated : 08 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Instagram is a powerful social media tool that you can use for marketing or networking. However, to successfully do any of that you need to have a good heavy follower base. You can’t simply share a post with 50 followers and call it successful marketing. No matter what your goals are, having a large followers list is what everyone wishes for.

If you are looking to automate your interactions with the users so that you can gain more followers then you can do it with Python. Python, along with Selenium WebDriver will help you to automate interaction so that you can increase your Instagram followers easily.

In this article, we will go step-by-step and build a Python bot to help you grow your Instagram and finally have a larger audience to share your posts with. Let’s get started.

Increase your Instagram followers with Python Bot

Below are the steps by which we can increase Instagram followers with the help of a simple bot in Python:

Step 1. Setting Up the Environment

To get started, we first need to set up our environment. This Python bot will be completely working on Selenium as it is nothing but automation in the application. Thus, we install selenium first. You can run the following command in the terminal to begin the installation.

pip install selenium

Now, from Selenium, we import the webdriver and other important modules. We import time as we would require the system to delay the execution for a few seconds. We use the By module to extract the element by different class names, tags, ids, or Xpath. The keys module will help us to input the data from our side to the website while the bot is running.

WebDriverWait will help us wait will the elements are loaded on the page, it may cause an error. Here, webdriver plays an important part. It is a module that helps Selenium open a Chrome browser window. However, to make it work you first need to download the ChromeDriver executable and specify its path in your script. You can install it from here.

Python3




from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
import time


Now that you have all the prerequisites installed and imported we can move to the next step.

Step 2. Logging into Instagram

Since you need to increase the followers of your Instagram, you need to login. But here, we login using Selenium and not directly. In the code given above you specify your own username and password. This will open a Chrome browser window so that it can perform the specified task. Now, we are prepared for login and can begin the process.

When the login page opens, we find the input area for username and password and then fill up our details using the send_keys method. Once, all the details are filled in you can locate the enter element by Xpath and click on it.

Python3




USERNAME = "YOUR USERNAME"
PASSWORD = "YOUR PASSWORD"
 
driver = webdriver.Chrome()
 
 
#Find username input area and write username
username = WebDriverWait(driver, timeout=60).until(
    lambda d: d.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[1]/div/label/input'))
username.send_keys(USERNAME)
 
#Find password input area and write password
password = driver.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[2]/div/label/input')
password.send_keys(PASSWORD)
 
#Click on Login Button
enter = driver.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[3]/button')
enter.click()
time.sleep(5)
 
#Click on Not Now in pop up
not_now = driver.find_element(By.CSS_SELECTOR, '._a9_1')
not_now.click()
time.sleep(5)


Output:

ezgifcom-jpg-to-png-converter

On logging in, Instagram sometimes asks the user if they want to save the log in information. This saved information is not going to help our bot next time when we run the script, hence we locate the “not now” button using CSS selector and click on it.

not-now-popup

Not Now Popup

Step 3. Search The User

Since, we have logged in now you need to find the user to access a followers list. We first locate the search icon in the sidebar on the instagram website. When we click on this search icon, we will have to find the search input element where you can input the name of the user you want to find. We locate the input area by using XPATH. Once, we input the username we get a list of suggestions below. We select the first element from that list and refer to it using found variable.

Python3




search = driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div[2]/div/div/div[1]/div[1]/div[1]/div/div/div/div/div[2]/div[2]/span/div/a')
search.click()
 
search_user = "USERNAME"
search_input = WebDriverWait(driver, timeout=60).until(
    lambda d: d.find_element(By.XPATH, '/html/body/div[2]/div/div/div[2]/div/div/div[1]/div[1]/div[1]/div/div/div[2]/div/div/div[2]/div/div/div[1]/div/div/input'))
search_input.send_keys(search_user)
found = WebDriverWait(driver, timeout=60).until(
    lambda d: d.find_element(By.XPATH, '/html/body/div[2]/div/div/div[2]/div/div/div[1]/div[1]/div[1]/div/div/div[2]/div/div/div[2]/div/div/div[2]/div/a/div[1]'))
found.click()


We need to assign the username as a string to the search_user variable instead of “USERNAME” placeholder. We will now click on the user account appearing first in the suggestion list. Thus, we have succesfully reached to an user’s account.

ezgifcom-png-to-jpg-converter-(2)

Step 4. Interacting with Followers

We can now start interacting with followers in the followers list. You need the below code for that. We first start by locating the followers list on the Instagram page and click on it. Then, we get a list of all the follow buttons that are currently visible on the screen.

Python3




followers = WebDriverWait(driver, timeout=60).until(lambda d: d.find_element(By.XPATH,
                                                                             '/html/body/div[2]/div/div/div[2]/div/div/div[1]/div[1]/div[2]/div[2]/section/main/div/header/section/ul/li[2]/a'))
followers.click()
 
time.sleep(10)
buttons = WebDriverWait(driver, timeout=60).until(
    lambda d: d.find_elements(By.CSS_SELECTOR, 'button._acan._acap._acas'))
time.sleep(5)
count = 0
while True:
    for btn in buttons:
        try:
            driver.execute_script("arguments[0].click();", btn)
            count += 1
            print(f"{count} buttons clicked")
            time.sleep(5)
        except:
            print("Skipped something")
            pass
    scr1 = WebDriverWait(driver, timeout=60).until(lambda d: d.find_element(By.XPATH,
                                                                            '/html/body/div[1]/div/div/div/div[2]/div/div/div[1]/div/div[2]/div/div/div/div/div[2]/div/div/div[2]'))
    time.sleep(5)
    driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", scr1)
    print("scrollled")
    buttons = WebDriverWait(driver, timeout=60).until(
        lambda d: d.find_elements(By.CSS_SELECTOR, 'button._acan._acap._acas'))


Inside the while loop we iterate through each of the button and click on it so that particular user would be followed by us. We keep a counter to count the number of users followed by the bot. Incase, if the button is unavailable or it is not clickable then we skip that button.

ezgifcom-png-to-jpg-converter-(3)

When all the users on the current page view have been followed we scroll the window by using driver.execute_script() method where we specify that we will scroll through the entire height of the window. Now, when we have scrolled, we will again get a list of buttons using CSS selector. Now, since this is present in the while loop, the process will continuously repeat the same steps. However, it is important to know that due to improved security and guideline by Instagram it doesn’t allow many of the accounts to be followed at once.

Video Demonstration

Conclusion

Thus, we have discussed an approach to increase Instagram followers by creating a Python bot. You should know that it is unethical to use the bot on the Instagram according to their rules. However, you can learn a lot of things by building this bot. Since, Instagram is constantly upgrading it’s security it will certainly detect your bot and mark it under suspicious activity. Hence, it is advisable that you use it only for the purpose of education.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads