|
| 1 | +from selenium import webdriver |
| 2 | +from getpass import getpass |
| 3 | +import time |
| 4 | + |
| 5 | +# Class for the bot |
| 6 | + |
| 7 | + |
| 8 | +class InstaBot: |
| 9 | + |
| 10 | + # Initializes bot |
| 11 | + def __init__(self): |
| 12 | + self.username = input('Enter your username:') |
| 13 | + self.pw = getpass('Enter your password(will NOT appear as you type):') |
| 14 | + self.PATH = r"C:\Program Files (x86)\chromedriver.exe" |
| 15 | + self.driver = webdriver.Chrome(self.PATH) |
| 16 | + |
| 17 | + # Starts Instagram |
| 18 | + def start(self): |
| 19 | + self.driver.get('https://www.instagram.com/') |
| 20 | + time.sleep(2) |
| 21 | + return |
| 22 | + |
| 23 | + # Logs into your account, also closes various dialogue boxes that open on |
| 24 | + # the way |
| 25 | + def login(self): |
| 26 | + |
| 27 | + user_field = self.driver.find_element_by_xpath( |
| 28 | + '//*[@id="loginForm"]/div/div[1]/div/label/input') |
| 29 | + pw_field = self.driver.find_element_by_xpath( |
| 30 | + '//*[@id="loginForm"]/div/div[2]/div/label/input') |
| 31 | + login_button = self.driver.find_element_by_xpath( |
| 32 | + '//*[@id="loginForm"]/div/div[3]/button/div') |
| 33 | + user_field.send_keys(self.username) |
| 34 | + pw_field.send_keys(self.pw) |
| 35 | + login_button.click() |
| 36 | + time.sleep(2.5) |
| 37 | + not_now1 = self.driver.find_element_by_xpath( |
| 38 | + '//*[@id="react-root"]/section/main/div/div/div/div/button') |
| 39 | + not_now1.click() |
| 40 | + time.sleep(2) |
| 41 | + not_now2 = self.driver.find_element_by_xpath( |
| 42 | + '/html/body/div[4]/div/div/div/div[3]/button[2]') |
| 43 | + not_now2.click() |
| 44 | + time.sleep(1) |
| 45 | + return |
| 46 | + |
| 47 | + # Opens your profile |
| 48 | + def open_profile(self): |
| 49 | + profile_link = self.driver.find_element_by_xpath( |
| 50 | + '//*[@id="react-root"]/section/main/section/div[3]' |
| 51 | + '/div[1]/div/div[2]/div[1]/a') |
| 52 | + profile_link.click() |
| 53 | + time.sleep(2) |
| 54 | + return |
| 55 | + |
| 56 | + # Opens the list of the people you follow |
| 57 | + def open_following(self): |
| 58 | + following_link = self.driver.find_element_by_xpath( |
| 59 | + '/html/body/div[1]/section/main/div/header/section/ul/li[3]/a') |
| 60 | + following_link.click() |
| 61 | + return |
| 62 | + |
| 63 | + # Gets the list of the people you follow |
| 64 | + def get_following(self): |
| 65 | + xpath = '/html/body/div[4]/div/div/div[2]' |
| 66 | + self.following = self.scroll_list(xpath) |
| 67 | + return |
| 68 | + |
| 69 | + # Opens the link to 'Followers' |
| 70 | + def open_followers(self): |
| 71 | + followers_link = self.driver.find_element_by_xpath( |
| 72 | + '//*[@id="react-root"]/section/main/div/header/section/ul/li[2]/a') |
| 73 | + followers_link.click() |
| 74 | + return |
| 75 | + |
| 76 | + # Gets the list of followers |
| 77 | + def get_followers(self): |
| 78 | + xpath = '/html/body/div[4]/div/div/div[2]' |
| 79 | + self.followers = self.scroll_list(xpath) |
| 80 | + return |
| 81 | + |
| 82 | + # Scrolls a scroll box and retrieves their names |
| 83 | + def scroll_list(self, xpath): |
| 84 | + |
| 85 | + time.sleep(2) |
| 86 | + scroll_box = self.driver.find_element_by_xpath(xpath) |
| 87 | + last_ht, ht = 0, 1 |
| 88 | + |
| 89 | + # Keep scrolling till you can't go down any further |
| 90 | + while last_ht != ht: |
| 91 | + last_ht = ht |
| 92 | + time.sleep(1) |
| 93 | + ht = self.driver.execute_script(""" |
| 94 | + arguments[0].scrollTo(0, arguments[0].scrollHeight); |
| 95 | + return arguments[0].scrollHeight; |
| 96 | + """, scroll_box) |
| 97 | + |
| 98 | + # Gets the list of accounts |
| 99 | + links = scroll_box.find_elements_by_tag_name('a') |
| 100 | + names = [name.text for name in links if name.text != ''] |
| 101 | + |
| 102 | + # Closes the box |
| 103 | + close_btn = self.driver.find_element_by_xpath( |
| 104 | + '/html/body/div[4]/div/div/div[1]/div/div[2]/button/div') |
| 105 | + close_btn.click() |
| 106 | + |
| 107 | + return names |
| 108 | + |
| 109 | + # Prints the list of people you follow who don't follow you back in |
| 110 | + # terminal |
| 111 | + def get_unfollowers(self): |
| 112 | + |
| 113 | + self.unfollowers = [ |
| 114 | + x for x in self.following if x not in self.followers] |
| 115 | + for name in self.unfollowers: |
| 116 | + print(name) |
| 117 | + return |
| 118 | + |
| 119 | + # Closes the driver |
| 120 | + def close(self): |
| 121 | + self.driver.quit() |
| 122 | + return |
| 123 | + |
| 124 | + |
| 125 | +def main(): |
| 126 | + |
| 127 | + # Bot method calls |
| 128 | + bb8 = InstaBot() |
| 129 | + |
| 130 | + bb8.start() |
| 131 | + |
| 132 | + bb8.login() |
| 133 | + |
| 134 | + bb8.open_profile() |
| 135 | + bb8.open_following() |
| 136 | + |
| 137 | + bb8.get_following() |
| 138 | + bb8.open_followers() |
| 139 | + |
| 140 | + bb8.get_followers() |
| 141 | + bb8.get_unfollowers() |
| 142 | + |
| 143 | + bb8.close() |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == '__main__': |
| 147 | + main() |
0 commit comments