-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfluencer_finder.py
104 lines (94 loc) · 4.09 KB
/
influencer_finder.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
#IMPORTS
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as expect
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import ElementClickInterceptedException
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.firefox.options import Options
import time
import sys
import os
#VARIABLES
influen = []
tag = 'dogsarethefunniest'
account_username = 'rsprivacc'
account_password = 'Raghav@2004'
DEBUG = False
#CODE_SETUP
options = Options()
options.headless = False
if DEBUG:
driver = webdriver.Firefox(executable_path='geckodriver.exe')
else:
driver = webdriver.Firefox(firefox_options=options, executable_path='geckodriver.exe')
#INITIATOR
driver.get('https://www.instagram.com/accounts/#/?next=%2F'+tag)
try:
WebDriverWait(driver, 50).until( expect.presence_of_element_located((By.XPATH, '//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[2]/div/label/input')))
print('Login Page Loaded')
except:
print("Page did not load. Try Again")
elem = driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[2]/div/label/input').send_keys(account_username)
elem = driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[3]/div/label/input').send_keys(account_password)
elem = driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[4]/button').click()
try:
WebDriverWait(driver, 30).until( expect.presence_of_element_located((By.CLASS_NAME, 'OfoBO ')))
print("Hashtag Page loaded")
except :
print("Page did not load. Maybe Wifi is too slow , for further information , try to replicate the error by turning debug = true.")
#COMMENTER
def COMMENTER(elem):
try:
influ = []
global counter
elem = driver.find_element_by_class_name('OfoBO').click()
time.sleep(0.5)
elem = driver.find_element_by_xpath('/html/body/div[1]/section/main/div/div[1]/div[1]/a').click()
elems = driver.find_elements_by_class_name('KV-D4')
res = [elems[i] for i in range(len(elems)) if i % 2 != 0]
for elem in res:
if elem.text not in influen and elem.text != "Verified":
influen.append(elem.text)
influ.append(elem.text)
return influ
except TimeoutException as ex:
print("A post took too lost to load and hence skipped")
solver = driver.find_element_by_class_name('wpO6b ').click()
except NoSuchElementException:
try:
elem = driver.find_element_by_xpath('/html/body/div[1]/section/main/div/div[2]/div[1]/a').click()
elems = driver.find_elements_by_class_name('KV-D4')
res = [elems[i] for i in range(len(elems)) if i % 2 != 0]
for elem in res:
if elem.text not in influen and elem.text != "Verified":
influen.append(elem.text)
influ.append(elem.text)
return influ
except NoSuchElementException:
try:
driver.refresh()
time.sleep(2)
elem = driver.find_element_by_xpath('/html/body/div[1]/section/main/div/div[2]/div[1]/a').click()
elems = driver.find_elements_by_class_name('KV-D4')
res = [elems[i] for i in range(len(elems)) if i % 2 != 0]
for elem in res:
if elem.text not in influen and elem.text != "Verified":
influen.append(elem.text)
influ.append(elem.text)
return influ
except NoSuchElementException:
print("Error")
except IndexError:
print("Page did not load properly , reloading.")
while True:
inf = COMMENTER(elem)
for x in inf:
driver.get('https://www.instagram.com/accounts/#/?next=%2F'+x)
infl = COMMENTER(elem)
with open('influencers.txt','w') as influencers:
for el in influen:
influencers.write(el + '\n')
driver.get('https://www.instagram.com/accounts/#/?next=%2F'+inf[-1])