Explain Codes LogoExplain Codes Logo

Downloading a picture via urllib and python

python
prompt-engineering
scraping
requests
Nikita BarsukovbyNikita Barsukov·Feb 6, 2025
TLDR

To download an image in Python using urllib, you can use the following snippet:

import urllib.request # Image URL you want to download img_url = 'http://example.com/image.jpg' # Local filename to save the downloaded image file_path = 'local_image.jpg' # The actual magic happening (or not, this is Python, not Hogwarts) urllib.request.urlretrieve(img_url, file_path)

Execute the above snippet to nab that img_url and save it as local_image.jpg on your machine.

Batch downloading images

If you are dealing with a large sum of images, Python can still be your perfectly programmable photo-picker. Utilize a for loop and make sure to include a try-except block to avoid any heartache from broken links:

import urllib.request # List of URLs as long as a CVS receipt image_urls = ['http://example.com/image1.jpg', 'http://example.com/image2.jpg', ...] # Directory to hold all these newfound pictures directory = '/path/to/your/directory/' for url in image_urls: # The good ol' slice and dice for file naming filename = directory + url.split('/')[-1] try: urllib.request.urlretrieve(url, filename) except Exception as e: # Who knew that picture didn't want to be downloaded? print(f'Failed to download {url}: {e}')

This will download each image to the directory of your preference.

Scraping for downloads

When you don't have a list of images on hand but a webpage full of them instead, we mix urllib with scraping to get our photos:

import urllib.request from bs4 import BeautifulSoup import requests # An art fortress/galery in URL form webpage_url = 'http://example.com/gallery' # More like "quest" am I right? page = requests.get(webpage_url) # BeautifulSoup to the rescue! soup = BeautifulSoup(page.content, 'html.parser') # Find all tags partying as an 'img' image_tags = soup.find_all('img') # Where to store these masterpieces directory = '/path/to/your/directory/' for tag in image_tags: # Collect 'src' like it's PokeMon img_url = tag['src'] # Really hoping there's no Picasso in this batch filename = directory + img_url.split('/')[-1] try: urllib.request.urlretrieve(img_url, filename) except Exception as e: # Not all works of art want to be saved print(f'Failed to download {img_url}: {e}')

Ensure to abide by the website's robots.txt rules and copyright laws in your pursuit of images.

Double-checking with requests

requests library can be your knight in shining armor with its advanced error checking features:

import requests img_url = 'http://example.com/image.jpg' file_path = 'local_image.jpg' response = requests.get(img_url, stream=True) if response.status_code == 200: with open(file_path, 'wb') as file: # File writes in bite-sized chunks - adorable! for chunk in response.iter_content(1024): file.write(chunk) else: # No response is bad response. Well, any response unlike 200. print(f'Image could not be retrieved: Status code {response.status_code}')

stream=True saves you from memory exhaust, thanks to iter_content method dealing with large images in bite-sized chunks.