Episode 29 of 29
Downloading Images
Learn how to download images from the web using Python and the requests library.
Let us learn how to download images from the internet using Python!
Installing Requests
pip install requestsDownloading an Image
import requests
url = "https://example.com/image.jpg"
response = requests.get(url)
with open("downloaded.jpg", "wb") as file:
file.write(response.content)
print("Image downloaded!")Downloading Multiple Images
import requests
import os
urls = [
"https://example.com/img1.jpg",
"https://example.com/img2.jpg",
]
os.makedirs("images", exist_ok=True)
for i, url in enumerate(urls):
response = requests.get(url)
filename = f"images/img_{i+1}.jpg"
with open(filename, "wb") as file:
file.write(response.content)
print(f"Downloaded: {filename}")What You Have Learned
Congratulations on completing the Python 3 Tutorial for Beginners! You now have a solid foundation to build real projects and explore advanced topics like web development, data science, and machine learning.