Categories
Python Answers

How to download a picture via urllib and Python?

Spread the love

Sometimes, we want to download a picture via urllib and Python.

In this article, we’ll look at how to download a picture via urllib and Python.

How to download a picture via urllib and Python?

To download a picture via urllib and Python, we can use the urllib.request.urlretrieve method.

For instance, we write:

import urllib.request

urllib.request.urlretrieve(
    "https://i.picsum.photos/id/1055/200/300.jpg?hmac=RkWuwIAp7D4g_2_g01yQSz2pdKBzYeFDEjq86_12OHg",
    "pic.jpg")

to call urllib.request.urlretrieve with the picture URL and the file path to save the picture to.

A GET request will then be made to download the image.

Conclusion

To download a picture via urllib and Python, we can use the urllib.request.urlretrieve method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *