Sometimes, we try to approach a task in multiple different ways.
Today, let’s code in Python to save an image file in the web using Python library ‘Requests’.
First of all, if you have not installed this Requests library before, you can easily do that right now. Just type a line of code below in your terminal window.
pip install requests
If you are using Windows and if you lack ‘pip’, then consult this website.
Let’s start!
First of all, we will load our dependent library ‘Request’.
import requests
Then, we will give the URL as an input to the ‘get’ method under the library.
r = requests.get("https://thumbs.dreamstime.com/z/seoul-architecture-line-skyline-illustration-linear-vector-cityscape-famous-landmarks-city-sights-design-icons-seoul-101576349.jpg")
Then, we will save this image in the binary format.
with open("test.jpg", "wb") as f:
f.write(r.content)
If we run these three lines of code, we get the image above in the directory we ran the python file in.
Easy-Peasy!
Happy learning! 😃