-
Notifications
You must be signed in to change notification settings - Fork 0
/
instagram_driver.py
112 lines (96 loc) · 4.04 KB
/
instagram_driver.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
import time
from selenium import webdriver
from instascrape import Profile
class InstagramDriver():
def __init__(self, username, password) -> None:
self.username = username
self.password = password
self.driver = self.get_webdriver()
self.complete_login()
self._retrieve_cookies()
def get_all_followers(self):
followers = {}
self.driver.get('https://www.instagram.com/imperiumpiekna_atelier/')
self.driver.find_element_by_xpath(
'//*[@id="react-root"]/section/main/div/header/section/ul/li[2]/a'
).click()
time.sleep(2)
divs = self.driver.find_elements_by_tag_name("div")
follower_dialog = list(
filter(lambda elem: elem.get_attribute("role") == "dialog",
divs))[0]
follower_list = follower_dialog.find_elements_by_tag_name("li")
# TODO retrieve all followers for account
# TODO implement scrolling until end of list
for follower in follower_list:
buttons = follower.find_elements_by_tag_name("button")
remove_button = list(
filter(lambda elem: elem.text == "Remove", buttons))[0]
username = follower.find_elements_by_tag_name('span')
username = list(filter(lambda elem: len(elem.text) > 0,
username))[0]
followers[username.text] = remove_button
return followers
def scrape_users(self, usernames: list):
users = []
for username in usernames:
profile = Profile(username)
profile.scrape(headers=self.headers, webdriver=self.driver)
users.append(profile)
return users
def get_webdriver(self):
self.driver = webdriver.Chrome("/Users/ihologa/Documents/chromedriver")
self.driver.get('http://www.instagram.com/')
time.sleep(2)
return self.driver
def accept_cockies(self):
cockies_prompt_accept = self.driver.find_element_by_xpath(
"/html/body/div[2]/div/div/div/div[2]/button[1]")
cockies_prompt_accept.click()
def log_in(self):
login_form = self.driver.find_element_by_xpath(
'//*[@id="loginForm"]/div/div[1]/div/label/input')
login_form.send_keys(self.username)
login_form2 = self.driver.find_element_by_xpath(
'//*[@id="loginForm"]/div/div[2]/div/label/input')
login_form2.send_keys(self.password)
log_in_button = self.driver.find_element_by_xpath(
'//*[@id="loginForm"]/div/div[3]/button')
log_in_button.click()
def remember_login(self, on=True):
time.sleep(3)
all_buttons = self.driver.find_elements_by_tag_name("button")
if on:
button = list(
filter(lambda elem: elem.text == "Save Info", all_buttons))[0]
else:
button = list(
filter(lambda elem: elem.text == "Not Now", all_buttons))[0]
button.click()
def set_notifications(self, on=False):
time.sleep(3)
all_buttons = self.driver.find_elements_by_tag_name("button")
if on:
button = list(
filter(lambda elem: elem.text == "Turn On", all_buttons))[0]
else:
button = list(
filter(lambda elem: elem.text == "Not Now", all_buttons))[0]
button.click()
def complete_login(self):
self.accept_cockies()
self.log_in()
self.remember_login(on=True)
self.set_notifications(on=False)
def _retrieve_cookies(self):
all_cookies = self.driver.get_cookies()
session_id = list(
filter(lambda cookie: cookie["name"] == "sessionid",
all_cookies))[0]["value"]
self.headers = {
"user-agent":
"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Mobile Safari/537.36 Edg/87.0.664.57",
"cookie": f"sessionid={session_id};"
}
def close(self):
self.driver.quit()