-
Notifications
You must be signed in to change notification settings - Fork 1
/
image_downloder.py
223 lines (170 loc) · 4.83 KB
/
image_downloder.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# import required modules
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import selenium.common.exceptions
import time
from bs4 import BeautifulSoup as bs
import requests
import os
from credentials import mypass,myuser
# get instagram account credentials
username = myuser
password = mypass
# assign URL
url = 'https://instagram.com/' + \
input('Enter User Name Of User For Downloading Posts ')
# Get URL path
def path():
global chrome
# starts a new chrome session
# add path if required
chrome = webdriver.Chrome()
# Extract URL
def url_name(url):
# the web page opens up
chrome.get(url)
# webdriver will wait for 4 sec before throwing a
# NoSuchElement exception so that the element
# is detected and not skipped.
time.sleep(4)
# Login to access post
def login(username, your_password):
log_but = chrome.find_element_by_class_name("L3NKy")
time.sleep(2)
log_but.click()
time.sleep(4)
# finds the username box
usern = chrome.find_element_by_name("username")
# sends the entered username
usern.send_keys(username)
# finds the password box
passw = chrome.find_element_by_name("password")
# sends the entered password
passw.send_keys(your_password)
# sends the enter key
passw.send_keys(Keys.RETURN)
time.sleep(5.5)
# Find Not Now Button
notn = chrome.find_element_by_class_name("yWX7d")
notn.click()
time.sleep(3)
# Function to get content of first post
def first_post():
chrome.find_element_by_class_name("kIKUG").click()
time.sleep(2)
# Function to get next post
def next_post():
try:
nex = chrome.find_element_by_class_name(
"coreSpriteRightPaginationArrow")
return nex
except selenium.common.exceptions.NoSuchElementException:
return 0
# Download contenet of all posts
def download_allposts():
# open First Post
first_post()
user_name = url.split('/')[-
# check if folder corresponding to user name exist or not
if(os.path.isdir(user_name) == False):
# Create folder
os.mkdir(user_name)
# Check if Posts contains multiple images or videos
multiple_images = nested_check()
if multiple_images:
nescheck = multiple_images
count_img = 0
while nescheck:
elem_img = chrome.find_element_by_class_name('rQDP3')
# Function to save nested images
save_multiple(user_name+'/'+'content1.'+str(count_img), elem_img)
count_img += 1
nescheck.click()
nescheck = nested_check()
# pass last_img_flag True
save_multiple(user_name+'/'+'content1.' +
str(count_img), elem_img, last_img_flag=1)
else:
save_content('_97aPb', user_name+'/'+'content1')
c = 2
while(True):
next_el = next_post()
if next_el != False:
next_el.click()
time.sleep(1.3)
try:
multiple_images = nested_check()
if multiple_images:
nescheck = multiple_images
count_img = 0
while nescheck:
elem_img = chrome.find_element_by_class_name('rQDP3')
save_multiple(user_name+'/'+'content' +
str(c)+'.'+str(count_img), elem_img)
count_img += 1
nescheck.click()
nescheck = nested_check()
save_multiple(user_name+'/'+'content'+str(c) +
'.'+str(count_img), elem_img, 1)
else:
save_content('_97aPb', user_name+'/'+'content'+str(c))
except selenium.common.exceptions.NoSuchElementException:
print("finished")
return
else:
break
c += 1
# Function to save content of the current post
def save_content(class_name, img_name):
time.sleep(0.5)
try:
pic = chrome.find_element_by_class_name(class_name)
except selenium.common.exceptions.NoSuchElementException:
print("Either This user has no images or you haven't followed this user or something went wrong")
return
html = pic.get_attribute('innerHTML')
soup = bs(html, 'html.parser')
link = soup.find('video')
if link:
link = link['src']
else:
link = soup.find('img')['src']
response = requests.get(link)
with open(img_name, 'wb') as f:
f.write(response.content)
time.sleep(0.9)
# Function to save multiple posts
def save_multiple(img_name, elem, last_img_flag=False):
time.sleep(1)
l = elem.get_attribute('innerHTML')
html = bs(l, 'html.parser')
biglist = html.find_all('ul')
biglist = biglist[0]
list_images = biglist.find_all('li')
if last_img_flag:
user_image = list_images[-1]
else:
user_image = list_images[(len(list_images)//2)]
video = user_image.find('video')
if video:
link = video['src']
else:
link = user_image.find('img')['src']
response = requests.get(link)
with open(img_name, 'wb') as f:
f.write(response.content)
# Function to check if the post is nested
def nested_check():
try:
time.sleep(1)
nes_nex = chrome.find_element_by_class_name('coreSpriteRightChevron ')
return nes_nex
except selenium.common.exceptions.NoSuchElementException:
return 0
# Driver Code
path()
time.sleep(1)
url_name(url)
login(username, password)
download_allposts()
chrome.close()