-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
137 lines (108 loc) · 4.26 KB
/
main.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import os
import time
import wget
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import root
from basic import out, sub
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"}
TIMEOUT = 10
user = sub('enter the username')
def run(user):
out('setting options')
options = webdriver.Firefox()
#options.add_argument("--headless")
options.add_argument('--no-sandbox')
options.add_argument("--log-level=3")
options.add_experimental_option("mobileEmulation", mobile_emulation)
out('opening chrome')
bot = webdriver.Chrome(options=options)
bot.set_window_size(600, 1000)
bot.get('https://www.instagram.com/accounts/login/')
out('entering username')
# username
elem = WebDriverWait(bot, TIMEOUT).until(
EC.presence_of_element_located((
By.XPATH, '//*[@id="loginForm"]/div[1]/div[3]/div/label/input')))
elem.send_keys(root.insta_username)
out('entering password')
# password
elem = WebDriverWait(bot, TIMEOUT).until(
EC.presence_of_element_located((
By.XPATH, '//*[@id="loginForm"]/div[1]/div[4]/div/label/input')))
elem.send_keys(root.insta_password)
out('logining')
# login button
elem = WebDriverWait(bot, TIMEOUT).until(
EC.presence_of_element_located((
By.XPATH, '//*[@id="loginForm"]/div[1]/div[6]/button')))
elem.click()
out('skiping session')
elem = WebDriverWait(bot, TIMEOUT).until(
EC.presence_of_element_located((
By.XPATH, '/html/body/div[1]/section/main/div/div/section/div/button')))
elem.click()
out('skiping notifications')
elem = WebDriverWait(bot, TIMEOUT).until(
EC.presence_of_element_located((
By.XPATH, '/html/body/div[5]/div/div/div/div[3]/button[2]')))
elem.click()
out(f'redirecting to https://www.instgram.com/{user}')
bot.get(f'https://www.instgram.com/{user}')
out('advanced tools')
elem = WebDriverWait(bot, TIMEOUT).until(
EC.presence_of_element_located((
By.XPATH, '/html/body/div/div[2]/button[3]')))
elem.click()
out('proceeding link')
elem = WebDriverWait(bot, TIMEOUT).until(
EC.presence_of_element_located((
By.XPATH, '//*[@id="proceed-link"]')))
elem.click()
out('Getting info')
elem = WebDriverWait(bot, TIMEOUT).until(
EC.presence_of_element_located((
By.XPATH, '/html/body/div[1]/section/main/div/div[1]/span')))
out(f'Name : {elem.text}')
elem = WebDriverWait(bot, TIMEOUT).until(
EC.presence_of_element_located((
By.XPATH, '//*[@id="react-root"]/section/main/div/div[1]/div')))
out(f'Bio : {elem.text}')
elem = WebDriverWait(bot, TIMEOUT).until(
EC.presence_of_element_located((
By.XPATH, '//*[@id="react-root"]/section/main/div/ul/li[1]/div/span')))
out(f'Posts : {elem.text}')
try:
elem = WebDriverWait(bot, TIMEOUT).until(
EC.presence_of_element_located((
By.XPATH, '//*[@id="react-root"]/section/main/div/ul/li[2]/a/div/span')))
out(f'followers : {elem.text}')
elem = WebDriverWait(bot, TIMEOUT).until(
EC.presence_of_element_located((
By.XPATH, '//*[@id="react-root"]/section/main/div/ul/li[3]/a/div/span')))
out(f'following : {elem.text}')
except:
out('private account')
bot.execute_script("window.scrollTo(0, 4000);")
images = bot.find_elements(By.TAG_NAME, value='img')
images = [image.get_attribute('src') for image in images]
images = images[:-2]
out('Number of scraped images: ' + str(len(images)))
if len(images) != 0:
path = os.getcwd()
path = os.path.join(path, 'Photos/'+user+'/')
os.mkdir(path)
out(path)
counter = 0
for image in images:
save_as = os.path.join(path, f'{user}_{str(counter)}.jpg')
wget.download(image, save_as)
counter += 1
bot.quit()
else:
bot.quit()
run(user)