|
| 1 | +import selenium |
| 2 | +from selenium import webdriver |
| 3 | +from webdriver_manager.chrome import ChromeDriverManager |
| 4 | +from time import sleep |
| 5 | + |
| 6 | +username = "" |
| 7 | +password = "" |
| 8 | +driver = None |
| 9 | + |
| 10 | +def open_instagram(): |
| 11 | + # set url to driver |
| 12 | + driver.get("https://instagram.com") |
| 13 | + |
| 14 | +def login(): |
| 15 | + sleep(2) |
| 16 | + # find username field |
| 17 | + driver.find_element_by_xpath("//input[@name='username']").send_keys(username) |
| 18 | + # find password field |
| 19 | + driver.find_element_by_xpath("//input[@name='password']").send_keys(password) |
| 20 | + # find Log in button |
| 21 | + driver.find_element_by_xpath("//button[@type='submit']").click() |
| 22 | + sleep(5) |
| 23 | + # click on not now for saving password |
| 24 | + driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]").click() |
| 25 | + sleep(2) |
| 26 | + # click on not now for turning on notifications |
| 27 | + driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]").click() |
| 28 | + sleep(2) |
| 29 | + |
| 30 | +def get_unfollowers(): |
| 31 | + # go to profile page |
| 32 | + driver.find_element_by_xpath("//a[contains(@href,'{}')]".format(username)).click() |
| 33 | + sleep(5) |
| 34 | + # click on following |
| 35 | + driver.find_element_by_xpath("//a[contains(@href,'{}/following')]".format(username)).click() |
| 36 | + sleep(2) |
| 37 | + # Get the list of following accounts |
| 38 | + following = get_names(driver) |
| 39 | + # click on followers |
| 40 | + driver.find_element_by_xpath("//a[contains(@href,'{}/followers')]".format(username)).click() |
| 41 | + sleep(2) |
| 42 | + # Get the list of followers accounts |
| 43 | + followers = get_names(driver) |
| 44 | + # compare both lists to find the difference accounts |
| 45 | + not_following_back = [user for user in following if user not in followers] |
| 46 | + return not_following_back |
| 47 | + |
| 48 | +def get_names(): |
| 49 | + # keep scrolling till the end of scroll view |
| 50 | + scroll_box = driver.find_element_by_xpath("/html/body/div[4]/div/div/div[2]") |
| 51 | + last_ht, ht = 0, 1 |
| 52 | + while last_ht != ht: |
| 53 | + last_ht = ht |
| 54 | + sleep(1) |
| 55 | + ht = driver.execute_script(""" |
| 56 | + arguments[0].scrollTo(0, arguments[0].scrollHeight); |
| 57 | + return arguments[0].scrollHeight; |
| 58 | + """, scroll_box) |
| 59 | + links = scroll_box.find_elements_by_tag_name('a') |
| 60 | + names = [name.text for name in links if name.text != ''] |
| 61 | + # close button |
| 62 | + driver.find_element_by_xpath("/html/body/div[4]/div/div/div[1]/div/div[2]/button").click() |
| 63 | + return names |
| 64 | + |
| 65 | +def main(): |
| 66 | + global username |
| 67 | + global password |
| 68 | + global driver |
| 69 | + # Input from users |
| 70 | + username=input("Enter username:") |
| 71 | + password=input("Enter password:") |
| 72 | + # configuring webdriver |
| 73 | + driver = webdriver.Chrome(ChromeDriverManager().install()) |
| 74 | + # operations |
| 75 | + open_instagram() |
| 76 | + login() |
| 77 | + unfollowers = get_unfollowers() |
| 78 | + # output |
| 79 | + print("Total unFollowers : {}\n".format(len(unfollowers))) |
| 80 | + for i in unfollowers: |
| 81 | + print(i) |
| 82 | + print("\n") |
| 83 | + driver.close() |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + main() |
0 commit comments