Python 3 Selenium Instagram Automation API Bot to Like Posts and Images Web App Full Project For Beginners

Python 3 Selenium Instagram Automation API Bot to Like Posts and Images Web App Full Project For Beginners

 

Welcome folks today in this post we will be building python selenium instagram bot to like posts and images using selenium. All the full source code of the application is shown below.

 

 

 

Get Started

 

 

In order to get started you need to install the selenium library using the pip command as shown below

 

pip install selenium

 

And after this you also need to download the chrome driver for selenium library here

 

Now make an app.py file and copy paste the following code

 

app.py

 

from selenium import webdriver
from time import sleep
from secrets import pw


class InstaBot:
    def __init__(self, username, pw):
        self.driver = webdriver.Chrome()
        self.username = username
        self.driver.get("https://instagram.com")
        sleep(2)
        self.driver.find_element_by_xpath("//a[contains(text(), 'Log in')]")\
            .click()
        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(pw)
        self.driver.find_element_by_xpath('//button[@type="submit"]')\
            .click()
        sleep(4)
        self.driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]")\
            .click()
        sleep(2)

    def get_unfollowers(self):
        self.driver.find_element_by_xpath("//a[contains(@href,'/{}')]".format(self.username))\
            .click()
        sleep(2)
        self.driver.find_element_by_xpath("//a[contains(@href,'/following')]")\
            .click()
        following = self._get_names()
        self.driver.find_element_by_xpath("//a[contains(@href,'/followers')]")\
            .click()
        followers = self._get_names()
        not_following_back = [user for user in following if user not in followers]
        print(not_following_back)

    def _get_names(self):
        sleep(2)
        sugs = self.driver.find_element_by_xpath('//h4[contains(text(), Suggestions)]')
        self.driver.execute_script('arguments[0].scrollIntoView()', sugs)
        sleep(2)
        scroll_box = self.driver.find_element_by_xpath("/html/body/div[3]/div/div[2]")
        last_ht, ht = 0, 1
        while last_ht != ht:
            last_ht = ht
            sleep(1)
            ht = self.driver.execute_script("""
                arguments[0].scrollTo(0, arguments[0].scrollHeight); 
                return arguments[0].scrollHeight;
                """, scroll_box)
        links = scroll_box.find_elements_by_tag_name('a')
        names = [name.text for name in links if name.text != '']
        # close button
        self.driver.find_element_by_xpath("/html/body/div[3]/div/div[1]/div/div[2]/button")\
            .click()
        return names


my_bot = InstaBot('##instagramusername', "###instagrampassword###")
my_bot.get_unfollowers()

 

See also  Python 3 Script to Download File as Attachment in Browser Using HTML5 & Javascript Full Tutorial For Beginners

 

Now inside this python script we need to replace the username and password to automate the instagram bot to like the posts and images

 

Now run this python script by typing the below command

 

python app.py

 

Leave a Reply