Instagram Bot
Tags: webdev, python, productivity, tutorial
Published: 2020-04-06 14:16:26

{% github https://github.com/aletisunil/Automate-Instagram no-readme %}
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,

<b>Prerequisites: </b>

* Selenium
* Python
* [Webdriver](https://chromedriver.chromium.org/downloads)

Intially, we need selenium to automate, And we can check by executing those commands in your python IDLE or Code Editors
```python
import selenium
print selenium.__version__
```
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
<b>Step 1: </b>
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

<b>Step 2: </b>
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"

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

<b>Step 4: </b>
 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 <code>driver.get("https://www.instagram.com/"+OtherUserId)</code>
 After going in to user profile, we need to get number of posts so that we can iterate over the posts.

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

<b>Step 6: </b>
You need to enter ur username, password and username of other person to whom you want to like
Ex:
<code>Instabot('abcxcwe','dfasd32963','capdt')#username,password,username of other person</code>
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


```python
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
```

{% vimeo https://vimeo.com/404481764 %}

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.