|
| 1 | +from selenium import webdriver |
| 2 | +from selenium.webdriver.common.by import By |
| 3 | +from selenium.webdriver.support.ui import WebDriverWait |
| 4 | +from selenium.webdriver.support import expected_conditions as ec |
| 5 | +from selenium.webdriver.common.keys import Keys |
| 6 | +import time |
| 7 | +import csv |
| 8 | + |
| 9 | +already_follow = [] |
| 10 | + |
| 11 | +with open('follower.csv', 'r') as csv_file: |
| 12 | + csv_reader = csv.reader(csv_file) |
| 13 | + for row in csv_reader: |
| 14 | + if len(row) != 0: |
| 15 | + for element in row: |
| 16 | + already_follow.append(element) |
| 17 | + |
| 18 | + |
| 19 | +class InstagramBot: |
| 20 | + def __init__(self, username, password): |
| 21 | + self.browser = webdriver.Chrome("chromedriver.exe") |
| 22 | + |
| 23 | + self.username = username |
| 24 | + self.password = password |
| 25 | + |
| 26 | + self.browser.delete_all_cookies() |
| 27 | + self.browser.maximize_window() |
| 28 | + |
| 29 | + def wait_for_object(self, type, string): |
| 30 | + return WebDriverWait(self.browser, 3).until(ec.presence_of_element_located((type, string))) |
| 31 | + |
| 32 | + def wait_for_objects(self, type, string): |
| 33 | + return WebDriverWait(self.browser, 3).until(ec.presence_of_all_elements_located((type, string))) |
| 34 | + |
| 35 | + def login(self): |
| 36 | + self.browser.get("https://www.instagram.com") |
| 37 | + |
| 38 | + inputs = self.wait_for_objects(By.CSS_SELECTOR, '._2hvTZ.pexuQ.zyHYP') |
| 39 | + inputs[0].send_keys(self.username) |
| 40 | + inputs[1].send_keys(self.password) |
| 41 | + |
| 42 | + time.sleep(1) |
| 43 | + |
| 44 | + inputs[1].send_keys(Keys.ENTER) |
| 45 | + |
| 46 | + time.sleep(2) |
| 47 | + |
| 48 | + def follow_followers(self, root_name, number_follower): |
| 49 | + all_follower = [] |
| 50 | + |
| 51 | + time.sleep(2) |
| 52 | + |
| 53 | + self.browser.get(f"https://www.instagram.com/{root_name}/") |
| 54 | + |
| 55 | + time.sleep(2) |
| 56 | + |
| 57 | + followers = self.wait_for_objects(By.CSS_SELECTOR, '._81NM2') |
| 58 | + followers[1].click() |
| 59 | + |
| 60 | + for i in range(1, number_follower + 1): |
| 61 | + time.sleep(2) |
| 62 | + src1 = self.wait_for_object( |
| 63 | + By.XPATH, f'/html/body/div[5]/div/div/div[2]/ul/div/li[{i}]') |
| 64 | + self.browser.execute_script("arguments[0].scrollIntoView();", src1) |
| 65 | + time.sleep(5) |
| 66 | + |
| 67 | + follower_name = src1.text.split()[0] |
| 68 | + |
| 69 | + if follower_name in already_follow: |
| 70 | + number_follower += 1 |
| 71 | + continue |
| 72 | + else: |
| 73 | + all_follower.append(follower_name) |
| 74 | + |
| 75 | + with open('follower.csv', 'a') as csv_file: |
| 76 | + csv_writer = csv.writer(csv_file) |
| 77 | + csv_writer.writerow(all_follower) |
| 78 | + |
| 79 | + for follower in all_follower: |
| 80 | + self.browser.get(f"https://www.instagram.com/{follower}/") |
| 81 | + |
| 82 | + time.sleep(5) |
| 83 | + |
| 84 | + subscribe_buttons = self.wait_for_objects( |
| 85 | + By.XPATH, '//button[text()="Follow"]') |
| 86 | + subscribe_buttons[0].click() |
| 87 | + |
| 88 | + time.sleep(5) |
| 89 | + |
| 90 | + |
| 91 | +bot = InstagramBot(username="enter-your-username-here", password="enter-your-password-here") |
| 92 | +bot.login() |
| 93 | +# bot.like_hashtag('lifestyle', 3) |
| 94 | +bot.follow_followers("therock", 3) |
0 commit comments