-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsta.py
77 lines (65 loc) · 2.36 KB
/
Insta.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
# Instagram Album?
import time
import re
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import urllib.request
import os
import shutil
from selenium.webdriver.chrome.options import Options
import sys
import getpass
posts = re.compile(r'src="https://[/\S+/]*com')
def OpenBrowser():
driver = webdriver.Firefox()
return driver
def login(driver, username, password):
driver.get('https://www.instagram.com/accounts/login/')
time.sleep(2)
username_elem = driver.find_element_by_xpath('//*[@name="username"]')
username_elem.clear()
username_elem.send_keys(username)
pass_element = driver.find_element_by_xpath('//*[@name="password"]')
pass_element.send_keys(password)
pass_element.send_keys(Keys.RETURN)
time.sleep(2)
def GetAccount(account_name):
driver.get('https://www.instagram.com/' + account_name)
scroll_pause = 1
last_height = driver.execute_script("return document.body.scrollHeight")
big_list = []
test = time.time()
while True:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(scroll_pause)
new_height = driver.execute_script("return document.body.scrollHeight")
last_height = new_height
html_source = driver.page_source
pics = re.findall(posts, html_source)
for item in pics:
if item[5:] not in big_list:
big_list.append(item[5:])
print(big_list)
# if new_height == last_height:
# break
if time.time() - test > 120:
break
print(len(big_list))
return big_list
def SaveImages(big_list):
if not os.path.exists(account_name):
os.mkdir(account_name)
urllib.request.urlretrieve(big_list[0], account_name + '\\' + account_name + '.jpg')
count = len(big_list) - 1
for item in big_list:
if count != len(big_list) - 1:
urllib.request.urlretrieve(item, account_name + '\\' + str(count + 1) + '.jpg')
count -= 1
if __name__ == "__main__":
account_name = str(sys.argv[1])
username = input('Enter username: ')
password = getpass.getpass('Enter password: ')
driver = OpenBrowser()
login(driver, username, password)
big_list = GetAccount(account_name)
SaveImages(big_list)