-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsta-capture.py
More file actions
70 lines (55 loc) · 1.92 KB
/
Copy pathinsta-capture.py
File metadata and controls
70 lines (55 loc) · 1.92 KB
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
from splinter import Browser
from selenium import webdriver
from openpyxl import Workbook
import time
import requests
import shutil
import os
import sys
if len(sys.argv) == 1 :
print("Enter atleast one username as argument")
os.sys(exit())
for i in range(1, len(sys.argv)):
user = sys.argv[i]
wb = Workbook()
ws = wb.active
ws.title = "sheet"
driver = webdriver.Chrome()
user_url = "https://www.instagram.com/"+user
driver.get(user_url)
try:
j = driver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div/div/h2')
if j:
print("The account you are trying to access is private.")
os.sys(exit())
except:
print('')
print("Scrolling the feeds of "+user)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
elem = driver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div/a')
elem.click()
time.sleep(3)
try:
while driver.find_element_by_class_name('_anzsd'):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
driver.execute_script("window.scrollTo(0,0);")
time.sleep(0.5)
except:
print('Finished scrolling')
os.system("mkdir -p "+user+"/posts")
print("Saving posts of "+user)
imgs = driver.find_elements_by_class_name('_2di5p')
for x in range(len(imgs)):
caption = imgs[x].get_attribute("alt")
url = imgs[x].get_attribute("src")
ws.cell(row=x+1, column=1).value = caption
response = requests.get(url, stream=True)
try:
with open(user+"/posts/post_"+str(len(imgs)-x)+".jpg", "wb") as out_file:
shutil.copyfileobj(response.raw, out_file)
del response
except:
print("unable to save file")
wb.save(user+"/"+user+"_captions.xlsx")
driver.close()