Project Description
Your project for today is to build a small real-world Python app that downloads all images from a webpage.
You’ll write a script that:
takes any URL,
finds all the image elements on the page
downloads each image to a folder on your computer.
Our code in the Solution page works for multiple URLs. Here are two URL examples you can use and download the images inside them:
https://books.toscrape.com/
https://en.wikipedia.org/wiki/Tomato
This is the kind of small utility script that’s super handy in real life, for example:
Downloading a full image gallery from a documentation page or design portfolio
Saving sample product photos from a public catalog for analysis
Grabbing icon or asset sets to build a local library
In this project, you’ll use:
requests
to fetch the HTML content,BeautifulSoup
to parse the HTML and find all<img>
tags,and
urllib.parse.urljoin
to build full image URLs.
Expected Output
When you run your program, it should create a folder like downloaded_images and print out something like:
Downloaded: 710e0dff-efdb-417c-bd73-04a6c22307f9.jpg
Downloaded: 5b77ee93-3f60-42f3-bf55-77ff9509c232.jpg
...
✅ All images downloaded!
And you’ll find the image files neatly saved in the downloaded_images folder. Here are the images from the https://books.toscrape.com/ webpage:
💡 Hint
Not sure how to get started? Click the Show Hint button below to see how to fetch HTML, extract image URLs, and save them to files.
📝 The hint includes a small snippet on using requests
, BeautifulSoup
, and open()
to write binary image data.
𝌣 Solution
🔒 This solution is available to paid subscribers only.
✅ Shows a complete script with folder creation, safe URL joining, and progress printing.
🚀 Want to keep leveling up?
Browse 300+ projects at dailypythonprojects.substack.com/archive and unlock all solutions by subscribing. Build real Python skills daily and transform how you learn — one project at a time.
Here my function to download images from url:
import os
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin, urlparse
test_urls = [
"https://books.toscrape.com/",
"https://en.wikipedia.org/wiki/Tomato"
]
def download_images(url, output_folder="downloaded_images"):
domain = urlparse(url).netloc.replace(".", "_")
folder_path = os.path.join(output_folder, domain)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
img_tags = soup.find_all("img")
for img in img_tags:
img_url = urljoin(url, img["src"])
file_name = os.path.basename(urlparse(img_url).path)
img_data = requests.get(img_url).content
img_path = os.path.join(folder_path, file_name)
with open(img_path, "wb") as file:
file.write(img_data)
print(f"✅ Downloaded: {file_name}")
print(f"✅ All images downloaded!")
for url in test_urls:
download_images(url)