TIL Python Basics Day 52 - Instagram Follower Bot

이다연·2021년 2월 7일
0

Udemy Python Course

목록 보기
48/64

Selenium WebDriver and Execute JavaScript
https://pythonbasics.org/selenium-execute-javascript/

In Selenium WebDriver, there are two major methods to put an element into a visible area:
https://stackoverflow.com/questions/34562095/scrollintoview-vs-movetoelement

scroll
https://stackoverflow.com/questions/38041974/selenium-scroll-inside-of-popup-div

https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop

https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight

My code

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import ElementClickInterceptedException

CHROME_DRIVER_PATH = "C:\-\chromedriver.exe"
SIMILAR_ACCOUNT = "c"
IG_USER = "d-"
IG_PASSWORD = "-"

class InstaFollower:
    def __init__(self):
        self.driver = webdriver.Chrome(executable_path=CHROME_DRIVER_PATH)

    def login(self):
        self.driver.get("https://www.instagram.com/accounts/login/")
        time.sleep(2)
        user_id = self.driver.find_element_by_name("username")
        password = self.driver.find_element_by_name("password")
        user_id.send_keys(IG_USER)
        password.send_keys(IG_PASSWORD)
        time.sleep(2)
        password.send_keys(Keys.ENTER)
        time.sleep(5)
        popup_x = self.driver.find_element_by_css_selector("body > div.RnEpo.Yx5HN > div > div > div > div.mt3GC > button.aOOlW.HoLwm")
        popup_x.click()

    def find_followers(self):
        self.driver.get(f"https://www.instagram.com/{SIMILAR_ACCOUNT}")
        # self.driver.get("https://www.instagram.com/twizted_up/")

        click_followers = self.driver.find_element_by_xpath("//*[@id='react-root']/section/main/div/header/section/ul/li[2]")
        click_followers.click()
        time.sleep(3)
        follow_btn = self.driver.find_element_by_xpath("/html/body/div[5]/div/div/div[2]/ul/div/li[1]")
        follow_btn.click()
        time.sleep(3)

        #find all li elements in list
        try:
            while True:
                follower_popup = self.driver.find_element_by_xpath("//div[@class='isgrP']")
                self.driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", follower_popup)

        except ElementClickInterceptedException:
            cancel_btn = self.driver.find_element_by_xpath("//button[contains(.,'팔로우 취소')]")
            self.driver.execute_script("arguments[0].click();", cancel_btn)


#__________________________________________________________________________
        # follower_popup_list = self.driver.find_elements_by_xpath("//div[@class='isgrP']//li")
        # print("follower_popup_list len is {}".format(len(follower_popup_list)))
        # print("ended")


    def follow(self):
        buttons = self.driver.find_elements_by_xpath("//button[contains(.,'팔로우')]")
        for btn in buttons:
            # Use the Java script to click on follow because after the scroll down the buttons will be un clickeable unless you go to it's location
            self.driver.execute_script("arguments[0].click();", btn)
            time.sleep(3.5)


bot = InstaFollower()
bot.login()
bot.find_followers()

Angela's code

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import ElementClickInterceptedException
import time

CHROME_DRIVER_PATH = YOUR CHROM DRIVER PATH
SIMILAR_ACCOUNT = "buzzfeedtasty"
USERNAME = YOUR INSTAGRAM USERNAME
PASSWORD = YOUR INSTAGRAM PASSWORD


class InstaFollower:

    def __init__(self, path):
        self.driver = webdriver.Chrome(executable_path=path)

    def login(self):
        self.driver.get("https://www.instagram.com/accounts/login/")
        time.sleep(5)

        username = self.driver.find_element_by_name("username")
        password = self.driver.find_element_by_name("password")

        username.send_keys(USERNAME)
        password.send_keys(PASSWORD)

        time.sleep(2)
        password.send_keys(Keys.ENTER)

    def find_followers(self):
        time.sleep(5)
        self.driver.get(f"https://www.instagram.com/{SIMILAR_ACCOUNT}")

        time.sleep(2)
        followers = self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/header/section/ul/li[2]/a')
        followers.click()

        time.sleep(2)
        modal = self.driver.find_element_by_xpath('/html/body/div[4]/div/div/div[2]')
        for i in range(10):
            self.driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", modal)
            time.sleep(2)

    def follow(self):
        all_buttons = self.driver.find_elements_by_css_selector("li button")
        for button in all_buttons:
            try:
                button.click()
                time.sleep(1)
            except ElementClickInterceptedException:
                cancel_button = self.driver.find_element_by_xpath('/html/body/div[5]/div/div/div/div[3]/button[2]')
                cancel_button.click()


bot = InstaFollower(CHROME_DRIVER_PATH)
bot.login()
bot.find_followers()
bot.follow()

From Q&A of lecture

My second confusion is how the 'following' - clicking the follow button - was achieved AFTER scrolling the page? Does it mean scrolling the page makes all the follow buttons available for clicking? I assumed the find_followers() will scroll all the followers and by the time we call the follow(), we would be on the last page and not have achieved much with the following. This was not the case and I just want to understand why it is so.

Lastly, I was unable to follow some people despite clicking the follow button.....In other words, the button remained unactivated - blue. Why is that? Also, after a while, I started seeing these pop ups (find attached), is it normal? Or does instagram suspect a bot? I gave the 1 sec delay Angela advised.

I am not usually an instagram person so pardon any silly questions. I sure hope the questions re clear enough.

I hope I get a response to my questions because I have not been receiving response to some of my questions lately.

Thank you.

profile
Dayeon Lee | Django & Python Web Developer

2개의 댓글

comment-user-thumbnail
2022년 6월 5일

At long last, you know why having a strong social media presence, whether as an influencer or a company, means investing in Instagram likes and followers. As a consequence of the association, you'll observe a boost in your Instagram analytics and a rise in your brand's awareness.

답글 달기
comment-user-thumbnail
2022년 6월 5일

I tried for a long time to become popular in IS, but I did not succeed. Then I was advised to buy likes and followers. This is an easier format for me and my friends. This helps to increase the flow of new active users. I found a great service for myself, https://top4smm.com/instagram-likes. ALSO I prefer to flip through other people's blogs to find out what resonates with subscribers the most, and I repeat this for myself.

답글 달기