Throughout the previous videos, we have been using the request library.
We use it into different ways.
First, we tried to get the content of the Yahoo website and we did that by using the text property
as you see in here.
And then we use the request library.
To get Jason data.
And for that we use the JSON method as you see here.
Now in both those cases, what we got was text data, so non binary daughter.
But how do you get binary data?
So how do you get an image?
How can you download an image with three course library or a zip file from the web or any other file
that is not text?
That's what I'll show you in this video.
So let's go to the browser and perhaps go to a Wikipedia page just to get the link of a file.
For example, let's work on an image file.
So I'll work on this image here.
I will download this image with Python and the request library.
So for that I need to know the URL of the image because each file, each image which you see on websites,
they have a particular URL which locates the image in the server where the website is hosted.
To get that, you are really going to right click over this image and Wikipedia and go to open image
in new tab and that will open the image on your browser and you can copy the URL in the address bar.
So I'll copy that.
And since this is out of the context of our app, I'll just open a Python console and show you the code
in here.
So first I will create a URL variable to store the image in this variable.
Right?
So we have the URL variable containing the string pointing to the image.
So make sure that the image name ends with an extension, an image extension such as jpg.
That means the URL is correctly pointing to that image.
So once I have a URL, then I want to import requests.
Then I'll make a request object.
Or you can also say a response object, actually, because the requests that get method actually gives
you a response of the request.
So it makes more sense to call the variable request.
So here goes the URL.
So we that we have a response rate which doesn't look any, I think, exciting.
But we have this number 200 is an A.P. code, which means the response was successful.
Now, we used to work on the tax property earlier.
Both.
This works when you are loading text data such as web pages.
So web pages or text, but an image is not text.
So what you need to use is a response that content.
So these are points which make up the image.
And in the command line you can not see the actual image, but you see the actual row coat that makes
up the image.
So we need to get that code and write it in a file.
For that we use the open function, which you have learned before.
So with open image dot P and sorry.
JPG, it was a jpg file.
And we need to open this in right binary modes.
So for text files, when we created text files, we used to write just w, but this is a binary file.
It's not a text file.
So we need to write WB as file a column press enter.
We indent here for spaces and then we say file.
So that's variable.
That's right.
And here we write the response that content which points to these data to these numbers.
So with that you press enter, enter again and in the root directory of your project you should see
an image that JPG file.
So let's refresh, let's reload this here.
Here it is.
And that's the image.
So that's how you can download an image using a particular URL, the URL which points to the image.
So that's the code again, request that gets the URL.
And then.
You create an empty file and you populate that file with the contents of the response object and that's
it.
The same technique can be used for other files, so if you have the link to a file which is hosted on
some websites on the cloud anywhere on the web, then you can use basically the same code and use the
same WB mode here to create the file and it will work basically the same.
Of course, if it's a zip file, you should give here the correct extension in the file name with that.
Thanks a lot.
See you later.