Amazing-Python-Scripts
149 строк · 4.2 Кб
1from selenium import webdriver2from getpass import getpass3import time4
5# Class for the bot
6
7
8class InstaBot:9
10# Initializes bot11def __init__(self):12self.username = input('Enter your username:')13self.pw = getpass('Enter your password(will NOT appear as you type):')14self.PATH = r"C:\Program Files (x86)\chromedriver.exe"15self.driver = webdriver.Chrome(self.PATH)16
17# Starts Instagram18def start(self):19self.driver.get('https://www.instagram.com/')20time.sleep(2)21return22
23# Logs into your account, also closes various dialogue boxes that open on24# the way25def login(self):26
27user_field = self.driver.find_element_by_xpath(28'//*[@id="loginForm"]/div/div[1]/div/label/input')29pw_field = self.driver.find_element_by_xpath(30'//*[@id="loginForm"]/div/div[2]/div/label/input')31login_button = self.driver.find_element_by_xpath(32'//*[@id="loginForm"]/div/div[3]/button/div')33user_field.send_keys(self.username)34pw_field.send_keys(self.pw)35login_button.click()36time.sleep(2.5)37not_now1 = self.driver.find_element_by_xpath(38'//*[@id="react-root"]/section/main/div/div/div/div/button')39not_now1.click()40time.sleep(2)41not_now2 = self.driver.find_element_by_xpath(42'/html/body/div[4]/div/div/div/div[3]/button[2]')43not_now2.click()44time.sleep(1)45return46
47# Opens your profile48def open_profile(self):49profile_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')52profile_link.click()53time.sleep(2)54return55
56# Opens the list of the people you follow57def open_following(self):58following_link = self.driver.find_element_by_xpath(59'/html/body/div[1]/section/main/div/header/section/ul/li[3]/a')60following_link.click()61return62
63# Gets the list of the people you follow64def get_following(self):65xpath = '/html/body/div[4]/div/div/div[2]'66self.following = self.scroll_list(xpath)67return68
69# Opens the link to 'Followers'70def open_followers(self):71followers_link = self.driver.find_element_by_xpath(72'//*[@id="react-root"]/section/main/div/header/section/ul/li[2]/a')73followers_link.click()74return75
76# Gets the list of followers77def get_followers(self):78xpath = '/html/body/div[4]/div/div/div[2]'79self.followers = self.scroll_list(xpath)80return81
82# Scrolls a scroll box and retrieves their names83def scroll_list(self, xpath):84
85time.sleep(2)86scroll_box = self.driver.find_element_by_xpath(xpath)87last_ht, ht = 0, 188
89# Keep scrolling till you can't go down any further90while last_ht != ht:91last_ht = ht92time.sleep(1)93ht = self.driver.execute_script(94"""95arguments[0].scrollTo(0, arguments[0].scrollHeight);
96return arguments[0].scrollHeight;
97""", scroll_box)98
99# Gets the list of accounts100links = scroll_box.find_elements_by_tag_name('a')101names = [name.text for name in links if name.text != '']102
103# Closes the box104close_btn = self.driver.find_element_by_xpath(105'/html/body/div[4]/div/div/div[1]/div/div[2]/button/div')106close_btn.click()107
108return names109
110# Prints the list of people you follow who don't follow you back in111# terminal112def get_unfollowers(self):113
114self.unfollowers = [115x for x in self.following if x not in self.followers116]117for name in self.unfollowers:118print(name)119return120
121# Closes the driver122def close(self):123self.driver.quit()124return125
126
127def main():128
129# Bot method calls130bb8 = InstaBot()131
132bb8.start()133
134bb8.login()135
136bb8.open_profile()137bb8.open_following()138
139bb8.get_following()140bb8.open_followers()141
142bb8.get_followers()143bb8.get_unfollowers()144
145bb8.close()146
147
148if __name__ == '__main__':149main()150