Skip to content

Commit 8a1ba3c

Browse files
committed
added new program to download all image from a website url
1 parent fe9701f commit 8a1ba3c

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
*.csv
22
*.xlsx
33
*.json
4-
*.env
4+
*.env
5+
env
6+

download-website-image/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
images

download-website-image/app.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import os
2+
import requests
3+
from bs4 import BeautifulSoup
4+
from urllib.parse import urljoin, urlparse
5+
6+
def download_images(url, folder="images"):
7+
# Create folder if it doesn't exist
8+
os.makedirs(folder, exist_ok=True)
9+
10+
# Get page content
11+
response = requests.get(url)
12+
if response.status_code != 200:
13+
print(f"Failed to retrieve URL: {url}")
14+
return
15+
16+
# Parse HTML
17+
soup = BeautifulSoup(response.text, "html.parser")
18+
19+
# Find all image tags
20+
img_tags = soup.find_all("img")
21+
if not img_tags:
22+
print("No images found on the page.")
23+
return
24+
25+
for img in img_tags:
26+
img_url = img.get("src")
27+
if not img_url:
28+
continue
29+
30+
# Convert relative URLs to absolute
31+
img_url = urljoin(url, img_url)
32+
33+
# Get image filename
34+
parsed_url = urlparse(img_url)
35+
img_name = os.path.basename(parsed_url.path)
36+
37+
if not img_name:
38+
continue
39+
40+
# Download image
41+
img_data = requests.get(img_url).content
42+
img_path = os.path.join(folder, img_name)
43+
44+
# Save image
45+
with open(img_path, "wb") as f:
46+
f.write(img_data)
47+
48+
print(f"Downloaded: {img_name}")
49+
50+
# Example usage
51+
website_url = "https://themeholy.com/html/agenxe/demo/index.html" # Replace with your target URL
52+
download_images(website_url)

0 commit comments

Comments
 (0)