1
00:00:00,510 --> 00:00:04,700
Throughout the previous videos, we have been using the request library.

2
00:00:04,710 --> 00:00:06,600
We use it into different ways.

3
00:00:06,600 --> 00:00:14,540
First, we tried to get the content of the Yahoo website and we did that by using the text property

4
00:00:14,550 --> 00:00:15,630
as you see in here.

5
00:00:15,930 --> 00:00:19,020
And then we use the request library.

6
00:00:22,310 --> 00:00:23,900
To get Jason data.

7
00:00:23,900 --> 00:00:27,560
And for that we use the JSON method as you see here.

8
00:00:27,800 --> 00:00:34,310
Now in both those cases, what we got was text data, so non binary daughter.

9
00:00:34,910 --> 00:00:37,790
But how do you get binary data?

10
00:00:37,790 --> 00:00:39,640
So how do you get an image?

11
00:00:39,650 --> 00:00:46,190
How can you download an image with three course library or a zip file from the web or any other file

12
00:00:46,190 --> 00:00:47,870
that is not text?

13
00:00:48,410 --> 00:00:50,570
That's what I'll show you in this video.

14
00:00:51,290 --> 00:00:59,720
So let's go to the browser and perhaps go to a Wikipedia page just to get the link of a file.

15
00:00:59,720 --> 00:01:02,090
For example, let's work on an image file.

16
00:01:02,540 --> 00:01:04,519
So I'll work on this image here.

17
00:01:04,940 --> 00:01:08,650
I will download this image with Python and the request library.

18
00:01:08,660 --> 00:01:16,220
So for that I need to know the URL of the image because each file, each image which you see on websites,

19
00:01:16,220 --> 00:01:22,340
they have a particular URL which locates the image in the server where the website is hosted.

20
00:01:22,370 --> 00:01:27,860
To get that, you are really going to right click over this image and Wikipedia and go to open image

21
00:01:27,860 --> 00:01:35,840
in new tab and that will open the image on your browser and you can copy the URL in the address bar.

22
00:01:36,470 --> 00:01:37,700
So I'll copy that.

23
00:01:38,180 --> 00:01:47,300
And since this is out of the context of our app, I'll just open a Python console and show you the code

24
00:01:47,300 --> 00:01:48,080
in here.

25
00:01:48,500 --> 00:01:53,660
So first I will create a URL variable to store the image in this variable.

26
00:01:55,370 --> 00:01:55,640
Right?

27
00:01:55,640 --> 00:02:00,440
So we have the URL variable containing the string pointing to the image.

28
00:02:00,440 --> 00:02:07,340
So make sure that the image name ends with an extension, an image extension such as jpg.

29
00:02:07,370 --> 00:02:11,750
That means the URL is correctly pointing to that image.

30
00:02:12,230 --> 00:02:15,800
So once I have a URL, then I want to import requests.

31
00:02:17,870 --> 00:02:20,890
Then I'll make a request object.

32
00:02:20,900 --> 00:02:30,380
Or you can also say a response object, actually, because the requests that get method actually gives

33
00:02:30,380 --> 00:02:32,750
you a response of the request.

34
00:02:32,750 --> 00:02:35,900
So it makes more sense to call the variable request.

35
00:02:36,350 --> 00:02:37,910
So here goes the URL.

36
00:02:39,530 --> 00:02:45,590
So we that we have a response rate which doesn't look any, I think, exciting.

37
00:02:45,950 --> 00:02:52,400
But we have this number 200 is an A.P. code, which means the response was successful.

38
00:02:53,310 --> 00:02:59,540
Now, we used to work on the tax property earlier.

39
00:03:00,970 --> 00:03:01,540
Both.

40
00:03:01,540 --> 00:03:06,850
This works when you are loading text data such as web pages.

41
00:03:06,850 --> 00:03:10,930
So web pages or text, but an image is not text.

42
00:03:10,930 --> 00:03:14,500
So what you need to use is a response that content.

43
00:03:16,380 --> 00:03:20,010
So these are points which make up the image.

44
00:03:20,010 --> 00:03:27,120
And in the command line you can not see the actual image, but you see the actual row coat that makes

45
00:03:27,120 --> 00:03:27,900
up the image.

46
00:03:27,930 --> 00:03:33,030
So we need to get that code and write it in a file.

47
00:03:33,060 --> 00:03:36,430
For that we use the open function, which you have learned before.

48
00:03:36,450 --> 00:03:39,150
So with open image dot P and sorry.

49
00:03:39,150 --> 00:03:42,030
JPG, it was a jpg file.

50
00:03:42,720 --> 00:03:46,570
And we need to open this in right binary modes.

51
00:03:46,590 --> 00:03:54,690
So for text files, when we created text files, we used to write just w, but this is a binary file.

52
00:03:54,690 --> 00:03:55,620
It's not a text file.

53
00:03:55,620 --> 00:04:01,530
So we need to write WB as file a column press enter.

54
00:04:01,650 --> 00:04:05,310
We indent here for spaces and then we say file.

55
00:04:05,310 --> 00:04:06,480
So that's variable.

56
00:04:06,870 --> 00:04:08,100
That's right.

57
00:04:08,100 --> 00:04:16,050
And here we write the response that content which points to these data to these numbers.

58
00:04:17,070 --> 00:04:25,140
So with that you press enter, enter again and in the root directory of your project you should see

59
00:04:25,140 --> 00:04:27,230
an image that JPG file.

60
00:04:27,240 --> 00:04:30,270
So let's refresh, let's reload this here.

61
00:04:30,300 --> 00:04:31,020
Here it is.

62
00:04:31,710 --> 00:04:32,760
And that's the image.

63
00:04:34,380 --> 00:04:41,940
So that's how you can download an image using a particular URL, the URL which points to the image.

64
00:04:44,400 --> 00:04:48,060
So that's the code again, request that gets the URL.

65
00:04:48,780 --> 00:04:49,680
And then.

66
00:04:53,060 --> 00:05:01,550
You create an empty file and you populate that file with the contents of the response object and that's

67
00:05:01,550 --> 00:05:01,910
it.

68
00:05:02,600 --> 00:05:08,990
The same technique can be used for other files, so if you have the link to a file which is hosted on

69
00:05:08,990 --> 00:05:15,320
some websites on the cloud anywhere on the web, then you can use basically the same code and use the

70
00:05:15,320 --> 00:05:21,080
same WB mode here to create the file and it will work basically the same.

71
00:05:22,040 --> 00:05:28,310
Of course, if it's a zip file, you should give here the correct extension in the file name with that.

72
00:05:28,340 --> 00:05:29,090
Thanks a lot.

73
00:05:29,120 --> 00:05:29,660
See you later.

