forked from redianmarku/instagram-follower-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
103 lines (70 loc) · 3.07 KB
/
run.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
import os
import time
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager as CM
# Complete these 2 fields ==================
USERNAME = 'your instagram username'
PASSWORD = 'your instagram password'
# ==========================================
TIMEOUT = 15
def scrape():
usr = input('[Required] - Whose followers do you want to scrape: ')
user_input = int(
input('[Required] - How many followers do you want to scrape (60-500 recommended): '))
options = webdriver.ChromeOptions()
# options.add_argument("--headless")
options.add_argument('--no-sandbox')
options.add_argument("--log-level=3")
mobile_emulation = {
"userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/90.0.1025.166 Mobile Safari/535.19"}
options.add_experimental_option("mobileEmulation", mobile_emulation)
bot = webdriver.Chrome(executable_path=CM().install(), options=options)
bot.set_window_size(600, 1000)
bot.get('https://www.instagram.com/accounts/login/')
time.sleep(2)
print("[Info] - Logging in...")
user_element = WebDriverWait(bot, TIMEOUT).until(
EC.presence_of_element_located((
By.XPATH, '//*[@id="loginForm"]/div[1]/div[3]/div/label/input')))
user_element.send_keys(USERNAME)
pass_element = WebDriverWait(bot, TIMEOUT).until(
EC.presence_of_element_located((
By.XPATH, '//*[@id="loginForm"]/div[1]/div[4]/div/label/input')))
pass_element.send_keys(PASSWORD)
login_button = WebDriverWait(bot, TIMEOUT).until(
EC.presence_of_element_located((
By.XPATH, '//*[@id="loginForm"]/div[1]/div[6]/button')))
time.sleep(0.4)
login_button.click()
time.sleep(5)
bot.get('https://www.instagram.com/{}/'.format(usr))
time.sleep(3.5)
WebDriverWait(bot, TIMEOUT).until(
EC.presence_of_element_located((
By.XPATH, '//*[@id="react-root"]/section/main/div/ul/li[2]/a'))).click()
time.sleep(2)
print('[Info] - Scraping...')
users = set()
for _ in range(round(user_input // 10)):
ActionChains(bot).send_keys(Keys.END).perform()
time.sleep(2)
followers = bot.find_elements_by_xpath(
'//*[@id="react-root"]/section/main/div/ul/div/li/div/div[1]/div[2]/div[1]/a')
# Getting url from href attribute
for i in followers:
if i.get_attribute('href'):
users.add(i.get_attribute('href').split("/")[3])
else:
continue
print('[Info] - Saving...')
print('[DONE] - Your followers are saved in followers.txt file!')
with open('followers.txt', 'a') as file:
file.write('\n'.join(users) + "\n")
if __name__ == '__main__':
scrape()