-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdownloude_img.py
More file actions
64 lines (55 loc) · 1.56 KB
/
downloude_img.py
File metadata and controls
64 lines (55 loc) · 1.56 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
import os
import requests
from bs4 import *
#-------------------------------------------------
def create_folder(imgs):
try:
folder_name = input("enter folder name : ")
os.mkdir(folder_name)
except:
print("Folder Exist with that name!")
create_folder()
finally:
dl_img(imgs, folder_name)
#---------------------------------------------
def dl_img(imgs, folder):
count_img= 0
print(f"{len(imgs)} found :))")
if len(imgs) != 0:
for i, img in enumerate(imgs):
try:
img_link = img['data-srcset']
except:
try:
img_link = img['data-src']
except:
try:
img_link = img['data-fallback-src"']
except:
try:
img_link = img['src']
except:
pass
try:
r = requests.get(img_link).content
try:
r = str(r, 'utf-8')
except UnicodeDecodeError:
with open(f"{folder}/imgs{i+1}.jpg", "wb+") as f:
f.write(r)
count_img += 1
except:
pass
if count_img == len(imgs):
print("All imges Downlouded!")
else:
print(f"totla images downloude{count_img} and total images find in site{len(imgs)}")
#--------------------------------------------------------
def main(url_temp):
r = requests.get(url_temp)
soup = BeautifulSoup(r.text, 'html.parser')
imgs = soup.findAll('img')
create_folder(imgs)
#----------------------------------------------------------
url = input("Enter Url : ___")
main(url)