-
Notifications
You must be signed in to change notification settings - Fork 0
/
follow.py
82 lines (70 loc) · 3.22 KB
/
follow.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
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from webdriver_manager.chrome import ChromeDriverManager as CM
class InstagramBot():
def __init__(self, email, password):
self.browserProfile = webdriver.ChromeOptions()
self.browserProfile.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})
self.browser = webdriver.Chrome(executable_path=CM().install(), options=self.browserProfile)
self.email = email
self.password = password
def signIn(self):
self.browser.get('https://www.instagram.com/')
emailInput = self.browser.find_elements_by_css_selector('form input')[0]
passwordInput = self.browser.find_elements_by_css_selector('form input')[1]
emailInput.send_keys(self.email)
passwordInput.send_keys(self.password)
passwordInput.send_keys(Keys.ENTER)
time.sleep(2)
def followWithUsername(self, username):
self.browser.get('https://www.instagram.com/' + username + '/')
time.sleep(2)
followButton = self.browser.find_element_by_css_selector('button')
if (followButton.text != 'Following'):
followButton.click()
time.sleep(2)
else:
print("You are already following this user")
def unfollowWithUsername(self, username):
self.browser.get('https://www.instagram.com/' + username + '/')
time.sleep(2)
followButton = self.browser.find_element_by_css_selector('button')
if (followButton.text == 'Following'):
followButton.click()
time.sleep(2)
confirmButton = self.browser.find_element_by_xpath('//button[text() = "Unfollow"]')
confirmButton.click()
else:
print("You are not following this user")
def getUserFollowers(self, username, max):
time.sleep(5)
self.browser.get('https://www.instagram.com/' + username + '/')
#followersLink = self.browser.find_element_by_css_selector('ul li a')
#followersLink.click()
#
#/html/body/div[1]/section/main/div/ul/li[2]/a
time.sleep(30)
followersList = self.browser.find_element_by_class_name('-nal3')
numberOfFollowersInList = len(followersList.find_elements_by_css_selector('li'))
followersList.click()
actionChain = webdriver.ActionChains(self.browser)
while (numberOfFollowersInList < max):
actionChain.key_down(Keys.SPACE).key_up(Keys.SPACE).perform()
numberOfFollowersInList = len(followersList.find_elements_by_css_selector('li'))
print(numberOfFollowersInList)
followers = []
for user in followersList.find_elements_by_css_selector('li'):
userLink = user.find_element_by_css_selector('a').get_attribute('href')
print(userLink)
followers.append(userLink)
if (len(followers) == max):
break
return followers
def closeBrowser(self):
self.browser.close()
def __exit__(self, exc_type, exc_value, traceback):
self.closeBrowser()
bot = InstagramBot("devell.py","33025340jojo")
print( bot.signIn())
print( bot.getUserFollowers("cbum", 22))