-
Notifications
You must be signed in to change notification settings - Fork 0
/
instabot.py
124 lines (90 loc) · 3.91 KB
/
instabot.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#importing selenium module for using chrome( yo need to downlaod and place the chromedriver file in the same folder to import webdrver)
from selenium import webdriver
#importing time module
from time import sleep
#importing keys to scroll end of a page sending keys
from selenium.webdriver.common.keys import Keys
# creating intsabot class as user name and password class attributes
# placing time delay in between for time taken to load pages
#creating class logs in on insta and opens the profile page
class Instabot:
def __init__(self,user_name,pwd):
self.user_name=user_name
self.driver=webdriver.Chrome()
self.driver.get("https://www.instagram.com/")
sleep(5)
user_name_link=self.driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[2]/div/label/input")
user_name_link.send_keys((user_name))
pw_=self.driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[3]/div/label/input")
pw_.send_keys(pwd)
login=self.driver.find_element_by_xpath('/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[4]/button/div')
login.click()
sleep(5)
profile_site=self.driver.get("https://www.instagram.com"+"/"+user_name+"/")
sleep(5)
#method to get followeing list from profile
def get_following(self):
sleep(2)
self.driver.find_element_by_xpath("//a[contains(@href,'/following')]")\
.click()
sleep(5)
a=['v']
b=['z']
#condition to scroll and check and input all followeing in the list
while a!=b:
b=a
sleep(5)
scroll=self.driver.find_element_by_xpath("//a[contains(@class,'notranslate')]")
scroll.send_keys(Keys.END)
sleep(5)
list_=self.driver.find_elements_by_xpath('/html/body/div[4]/div/div[2]/ul/div')
a=[x.text for x in list_]
sleep(5)
names=self.driver.find_elements_by_xpath("/html/body/div[4]/div/div[2]/ul/div/li")
name = [name.text for name in names]
followers_list=[x.split()[0] for x in name]
self.driver.get("https://www.instagram.com"+"/"+self.user_name+"/")
sleep(5)
return followers_list
#method to return followers list from profile
def get_followers(self):
sleep(2)
self.driver.find_element_by_xpath("//a[contains(@href,'/followers')]")\
.click()
sleep(5)
a=['v']
b=['z']
#condition to scroll and check and input all followers in the list
while a!=b:
b=a
sleep(5)
scroll=self.driver.find_element_by_xpath("//a[contains(@class,'notranslate')]")
scroll.send_keys(Keys.END)
sleep(5)
list_=self.driver.find_elements_by_xpath('/html/body/div[4]/div/div[2]/ul/div')
a=[x.text for x in list_]
sleep(5)
names=self.driver.find_elements_by_xpath("/html/body/div[4]/div/div[2]/ul/div/li")
name = [name.text for name in names if name!='']
following_list=[x.split()[0] for x in name]
self.driver.get("https://www.instagram.com"+"/"+ self.user_name +"/")
sleep(5)
return following_list
#methof to check followers and following list
#calls following and followers deifinition , place them it two list and
#checks the list with one another and returns list with names in following_list
#but not in followers list
def get_unfollowing (self):
not_following=[]
following=self.get_following()
sleep(5)
followers=self.get_followers()
sleep(5)
for i in following:
m=0
for j in followers:
if i==j:
m=1
if m==0:
not_following.append(i)
return (not_following)