Python Image Downloader: Download Images from a URL
Image Downloader:
Build a program that downloads images from a given URL.
Input values:
User provides the URL of a website containing images to be downloaded
Output value:
Images are downloaded from the specified URL and saved to a local directory.
Example:
Input values: Enter the URL of the website with images: https://example.com/images Output value: Images downloaded successfully and saved to the local directory.
Here are two different solutions for an "Image Downloader" program in Python. This program will take a URL from the user, find all images on the specified webpage, and download them to a local directory.
Prerequisites:
Before running the code, ensure that the following libraries are installed:
pip install requests beautifulsoup4
Solution 1: Basic Approach using 'requests' and 'BeautifulSoup'
Code:
Output:
(base) C:\Users\ME>python test.py Enter the URL of the website with images: https://www.wikipedia.org/ Invalid URL found: portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png Downloaded: Wikimedia_Foundation_logo_-_wordmark.svg
Explanation:
- Uses requests to send HTTP GET requests to the specified URL and BeautifulSoup to parse HTML content and extract image URLs.
- Defines two functions:
- download_image(url, folder_path): Downloads an individual image from a given URL and saves it to a local directory.
- download_images_from_url(website_url, folder_path): Downloads all images from the specified website.
- Checks if the img tags contain valid URLs and downloads only those starting with http:// or https://.
- Simple and effective, but lacks modularity for future improvements.
Solution 2: Using a Class-Based approach for better Organization
Prerequisites
Ensure that the following libraries are installed:
pip install requests beautifulsoup4
Code:
Output:
(base) C:\Users\ME>python test.py Enter the URL of the website with images: https://commons.wikimedia.org/wiki/Main_Page Downloaded: 500px-Castillo_de_Hohenwerfen%2C_Werfen%2C_Austria%2C_2019-05-17%2C_DD_143-149_PAN.jpg Downloaded: 14px-Magnify-clip_%28sans_arrow%29.svg.png Downloaded: 100px-Generic_Camera_Icon.svg.png Downloaded: 45px-Wikimedia-logo_black.svg.png Downloaded: 32px-Wikipedia-logo-v2.svg.png Downloaded: 32px-Wikinews-logo.svg.png Downloaded: 32px-Wiktionary-logo.svg.png Downloaded: 32px-Wikibooks-logo.svg.png Downloaded: 32px-Wikiquote-logo.svg.png ---------------------------------------- ----------------------------------------
Explanation:
- Encapsulates the image downloading logic in an 'ImageDownloader' class, making the code more modular and organized.
- The '__init__' method initializes the class with the URL and folder path where images will be saved.
- The 'download_image()' method handles downloading individual images, while 'download_all_images()' manages the overall downloading process.
- The class-based approach makes the code more reusable, scalable, and easier to maintain or extend with additional features.
Note:
Both solutions effectively implement an image downloader that retrieves images from a given URL and saves them to a local directory, with Solution 1 using a straightforward function-based approach and Solution 2 using a class-based design for better organization and extensibility.