-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
33 lines (31 loc) · 1.01 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
# ©Rishabh Tripathi (github.com/ristri)
from bs4 import BeautifulSoup
import urllib.request
from datetime import datetime
def main():
print("©Rishabh Tripathi (github.com/ristri)")
soup = html_source()
img_dl(soup)
def user_input():
url = input("Enter Website URL (with http://)-\n")
return url
def html_source():
req = urllib.request.urlopen(user_input()).read()
data= BeautifulSoup(req,"html.parser")
return data
def img_name():
return datetime.now().strftime("%Y%m%d%H%M%S.%f" + ".jpg")
def img_dl(soup):
i=0
for link in soup.findAll("img"):
img_url = link.get("src")
if check(img_url):
i+=1
image = urllib.request.urlopen(img_url)
with open(img_name(),'wb') as f:
f.write(image.read())
print("Downloaded %d" %i)
def check(img_url):
return (img_url.endswith(".jpg") or img_url.endswith(".jpeg")) and (img_url.startswith("http") or img_url.startswith("https"))
if __name__ == '__main__':
main()