-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
80 lines (67 loc) · 2.73 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from selenium import webdriver
from time import sleep
from secrets import usr, pw
class InstaBot:
def __init__(self, username, pw):
# Log in to Instagram with provided credentials
self.driver = webdriver.Chrome()
self.username = username
self.driver.get("https://instagram.com")
sleep(2)
self.driver.find_element_by_xpath(
"//button[contains(text(), 'Accept')]").click()
login = self.driver.find_element_by_xpath(
"//input[@name='username']").send_keys(username)
login = self.driver.find_element_by_xpath(
"//input[@name='password']").send_keys(pw)
login = self.driver.find_element_by_xpath(
"//button[@type='submit']").click()
sleep(4)
login = self.driver.find_element_by_xpath(
"//button[contains(text(), 'Not Now')]").click()
login = self.driver.find_element_by_xpath(
"//button[contains(text(), 'Not Now')]").click()
def get_unfollowers(self):
# Open the following and followers section for comparison
self.driver.find_element_by_xpath(
f"//a[contains(@href, '{self.username}')]").click()
sleep(1)
self.driver.find_element_by_xpath(
"//a[contains(@href, 'following')]").click()
following = self.get_names()
sleep(1)
self.driver.find_element_by_xpath(
"//a[contains(@href, 'followers')]").click()
followers = self.get_names()
# collect the users with list comprehension
not_following_back = [
user for user in following if user not in followers]
print(not_following_back)
def get_names(self):
sleep(1)
# get the full list
sugs = self.driver.find_element_by_xpath(
"//div[@role='dialog']")
sleep(1)
self.driver.execute_script('arguments[0].scrollIntoView()', sugs)
scroll_box = self.driver.find_element_by_xpath(
"/html/body/div[5]/div/div/div[2]")
last_ht, ht = 0, 1
# scrolling down and waiting for the element loading
while last_ht != ht:
last_ht = ht
sleep(1)
ht = self.driver.execute_script("""
arguments[0].scrollTo(0, arguments[0].scrollHeight);
return arguments[0].scrollHeight;
""", scroll_box)
links = scroll_box.find_elements_by_tag_name('a')
names = [name.text for name in links if name.text != '']
# close button
self.driver.find_element_by_xpath(
"/html/body/div[5]/div/div/div[1]/div/div[2]/button").click()
return names
if __name__ == '__main__':
my_Bot = InstaBot(usr, pw)
my_Bot.get_unfollowers()
sleep(5)