-
Notifications
You must be signed in to change notification settings - Fork 0
/
Instagram_follow.py
75 lines (60 loc) · 2.79 KB
/
Instagram_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
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from time import sleep
class InstaBot:
def __init__(self, username, password, search):
self.driver = webdriver.Chrome()
self.username = username
self.password = password
self.login()
self.search_user(search)
def login(self):
self.driver.get("https://instagram.com")
sleep(2)
# Log in to Instagram
self.driver.find_element(By.NAME, "username").send_keys(self.username)
self.driver.find_element(By.NAME, "password").send_keys(self.password)
self.driver.find_element(By.XPATH, '//button[@type="submit"]').send_keys(Keys.ENTER)
sleep(3)
# Handle pop-ups
self.handle_popups()
def handle_popups(self):
try:
not_now_button = self.driver.find_element(By.XPATH, '//button[text()="Not Now"]')
not_now_button.click()
sleep(3)
except Exception as e:
print("No 'Not Now' button found: ", e)
try:
turn_on_notifications_button = self.driver.find_element(By.XPATH, '//button[text()="Not Now"]')
turn_on_notifications_button.click()
sleep(3)
except Exception as e:
print("No 'Turn on Notifications' button found: ", e)
def search_user(self, search):
search_box = self.driver.find_element(By.XPATH, '//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/input')
search_box.send_keys(search)
sleep(4)
# Click on the first result
first_result = self.driver.find_element(By.CLASS_NAME, 'z556c')
first_result.click()
sleep(5)
def bulk_follow(self):
self.driver.find_element(By.XPATH, '//*[@id="react-root"]/section/main/div/header/section/ul/li[3]/a').send_keys(Keys.ENTER)
sleep(3)
scroll_box = self.driver.find_element(By.CSS_SELECTOR, "div[class='isgrP']")
number_of_following = 100 # Set the number of followings here
scrolling_times = (number_of_following / 4)
scroll_count = int(scrolling_times + 5)
for _ in range(scroll_count):
self.driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', scroll_box)
sleep(2)
buttons = self.driver.find_elements(By.XPATH, '//button[text()="Follow"]')
for btn in buttons:
if btn.text == 'Follow':
self.driver.execute_script("arguments[0].click();", btn)
sleep(1)
# Add the username, password, and search term
bot = InstaBot('your_username', 'your_password', 'search_term')
bot.bulk_follow()