Day 052

AWESOMee·2022년 3월 26일
0

Udemy Python Bootcamp

목록 보기
52/64
post-thumbnail

Udemy Python Bootcamp Day 052

Instagram Follower Bot

Create a Class

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

SERVICE = Service("/Users/****/Development/chromedriver")
SIMILAR_ACCOUNT = "******"
USERNAME = "*******"
PASSWORD = "*******"


class InstaFollower:
    def __init__(self, service):
        self.driver = webdriver.Chrome(service=service)

    def login(self):
        pass

    def find_followers(self):
        pass

    def follow(self):
        pass

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

Login to Instagram

    def login(self):
        time.sleep(2)
        username = self.driver.find_element(By.NAME, "username")
        username.send_keys(USERNAME)
        password = self.driver.find_element(By.NAME, "password")
        password.send_keys(PASSWORD)
        password.send_keys(Keys.ENTER)

        time.sleep(3)
        not_now_button = self.driver.find_element(By.XPATH, "//*[text()='Not now']")
        not_now_button.click()
        time.sleep(3)
        not_now_button = self.driver.find_element(By.XPATH, "//*[text()='Not Now']")
        not_now_button.click()

Find the followers of the target account

    def find_followers(self):
        time.sleep(3)
        self.driver.get(f"https://www.instagram.com/{SIMILAR_ACCOUNT}")
        followers = self.driver.find_element(By.XPATH, "//*[text()=' followers']")
        followers.click()
        time.sleep(1)
        follower_popup = self.driver.find_element(By.XPATH, "/html/body/div[6]/div/div/div/div[2]")
        for i in range(10):
            self.driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", follower_popup)
            time.sleep(2)

스크롤하는 것도 그냥 stack overflow보고 그대로 따라한거긴 한데
follower_popup full xpath찾는데 조금 힘들었다....

Follow all the followers

from selenium.common.exceptions import ElementClickInterceptedException
import random

RANDOM_NUM = random.randint(2, 10)

    def follow(self):
        follow_list = self.driver.find_elements(By.CLASS_NAME, "sqdOP")
        for follow in follow_list:
            try:
                follow.click()
                time.sleep(RANDOM_NUM)
            except ElementClickInterceptedException:
                cancel_button = self.driver.find_element(By.XPATH, "//*[text()='Cancel']")
                cancel_button.click()

솔루션 안보고 내가 해냄...!
근데 며칠전에 한 플젝에서 except error못잡아낸다고 했던거
import exception안해서 그런거였음.. 진짜 멍청...
질의응답보니까 stop_follow()만들어서 자동으로 멈추도록 만드시던데,,
난 여기까지만 할게.. 일단은..

FINAL

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import ElementClickInterceptedException
import time
import random

SERVICE = Service("/Users/****/Development/chromedriver")
SIMILAR_ACCOUNT = "celine"
USERNAME = "***********"
PASSWORD = "********"
RANDOM_NUM = random.randint(2, 10)


class InstaFollower:
    def __init__(self, service):
        self.driver = webdriver.Chrome(service=service)
        self.driver.get("https://www.instagram.com/accounts/login")

    def login(self):
        time.sleep(2)
        username = self.driver.find_element(By.NAME, "username")
        username.send_keys(USERNAME)
        password = self.driver.find_element(By.NAME, "password")
        password.send_keys(PASSWORD)
        password.send_keys(Keys.ENTER)

        # time.sleep(5)
        # not_now_button = self.driver.find_element(By.XPATH, "//*[text()='Not now']")
        # not_now_button.click()
        time.sleep(5)
        not_now_button = self.driver.find_element(By.XPATH, "//*[text()='Not Now']")
        not_now_button.click()

    def find_followers(self):
        time.sleep(3)
        self.driver.get(f"https://www.instagram.com/{SIMILAR_ACCOUNT}")
        followers = self.driver.find_element(By.XPATH, "//*[text()=' followers']")
        followers.click()
        time.sleep(1)
        follower_popup = self.driver.find_element(By.XPATH, "/html/body/div[6]/div/div/div/div[2]")
        for i in range(10):
            self.driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", follower_popup)
            time.sleep(2)

    def follow(self):
        follow_list = self.driver.find_elements(By.CLASS_NAME, "sqdOP")
        for follow in follow_list:
            try:
                follow.click()
                time.sleep(RANDOM_NUM)
            except ElementClickInterceptedException:
                cancel_button = self.driver.find_element(By.XPATH, "//*[text()='Cancel']")
                cancel_button.click()


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

login()에서 첫번째 not_now_button 왜 주석처리했냐면,
플젝하다가 중간에 로그인 아이디를 다른걸로 바꿨는데
기존꺼는 아이디 기억..?하는거였나 그 창이 먼저 뜨고 알림설정할까?하는 창이 떠서 그렇게 적었었는데
follow()만들면서 로그인 아이디를 바꿨더니 이 아이디는 또 아이디 기억할까? 물어보는 창이 안뜨고 알림설정할까? 물어보는 창만 두번 떴음
근데 코드는 한번만 적어도 자동으로 두번 다 not now버튼이 눌러지더라고..? 흠

profile
개발을 배우는 듯 하면서도

0개의 댓글