1
00:00:01,859 --> 00:00:09,450
In this video, you will learn how to download a file from a website with Python, for this example,

2
00:00:09,450 --> 00:00:13,560
we will download an MP free file from this website.

3
00:00:13,620 --> 00:00:18,600
File samples, dot com slash formats, slash and B3.

4
00:00:18,750 --> 00:00:25,200
You can also find this link in the resources, and I'll download this first file here.

5
00:00:25,680 --> 00:00:31,380
So for that, I need to know the link to the file and you see this button here.

6
00:00:31,380 --> 00:00:37,500
If I press this button, I will download the file through my browser, but I once downloaded through

7
00:00:37,500 --> 00:00:37,950
Python.

8
00:00:37,960 --> 00:00:43,710
So what I do is I want to know the link to the file, so I want to copy the link.

9
00:00:43,710 --> 00:00:45,600
So right click over the download button.

10
00:00:45,780 --> 00:00:49,200
Copy the link and keep it somewhere in your script.

11
00:00:49,650 --> 00:00:58,710
So that is the link pointing to the empty three file, so I'll give you therefore null and import requests.

12
00:00:59,440 --> 00:01:06,810
Request is a third party in the library for Python able to make, get and post requests.

13
00:01:07,140 --> 00:01:15,720
It gets requests is when you access a file or a web page, so if you download the file that is a gets

14
00:01:15,720 --> 00:01:16,200
request.

15
00:01:16,440 --> 00:01:19,620
A post request is when we want to upload the file.

16
00:01:20,280 --> 00:01:22,890
In our case, we want to use a get requests.

17
00:01:23,160 --> 00:01:31,650
So for that, we see requests that get so get this method and it gets as arguments.

18
00:01:31,950 --> 00:01:35,070
The URL, the URL is the URL to the file.

19
00:01:35,220 --> 00:01:40,530
So let me create a variable here you URL and this should be a string.

20
00:01:40,530 --> 00:01:43,560
So I'm using this double quotes there.

21
00:01:43,770 --> 00:01:45,030
So you URL goes here.

22
00:01:45,030 --> 00:01:51,090
And then if you just print a wreck, that content.

23
00:01:52,550 --> 00:01:53,630
And run the script.

24
00:01:57,890 --> 00:02:03,050
What we will have in the Commons line is the voice over the file.

25
00:02:03,260 --> 00:02:04,940
So this is a binary file.

26
00:02:04,950 --> 00:02:07,630
We have text files and we have binary files.

27
00:02:08,060 --> 00:02:14,930
Binary files are files that are not text files such as MP three in this case.

28
00:02:15,620 --> 00:02:25,440
Now what do we want to do with this binary file is we want to open a file using the open function and

29
00:02:25,440 --> 00:02:33,770
to give it a name such as don't load the MP three and we open that in right binary mode.

30
00:02:33,800 --> 00:02:38,570
So we are creating a new empty file with that name.

31
00:02:39,020 --> 00:02:45,020
And with this file handling methods, so W.B. means right binary.

32
00:02:45,530 --> 00:02:51,400
If you want to read the file you'd be using or B, we're talking about binary files again.

33
00:02:51,650 --> 00:02:56,390
So it would be what we want to use as a file and file.

34
00:02:56,420 --> 00:02:57,800
That's right.

35
00:02:58,010 --> 00:03:01,310
So we will write content to this new empty file.

36
00:03:02,090 --> 00:03:08,870
The content is this thing in here, Rick, that content.

37
00:03:09,710 --> 00:03:13,850
So what I'm going to do is I'm I'll create a new variable content.

38
00:03:14,390 --> 00:03:21,950
We just see you to read that content and then I provide that content variable to here.

39
00:03:23,120 --> 00:03:30,080
So I'm going to stop the execution of the previous run because it's too much work for Python to print

40
00:03:30,080 --> 00:03:32,300
it out, to print all these bites.

41
00:03:32,630 --> 00:03:35,450
And I'm not going to print out anything this time.

42
00:03:35,450 --> 00:03:37,370
So I deleted the print function.

43
00:03:37,760 --> 00:03:39,080
I'm just going to run this.

44
00:03:40,180 --> 00:03:42,550
And don't load that empty three is here.

45
00:03:43,120 --> 00:03:46,210
So if you click that's or downloaded.

46
00:03:47,960 --> 00:03:50,900
From here and then play with your major player.

47
00:03:57,550 --> 00:03:59,230
And that's a downloaded file.

48
00:04:03,170 --> 00:04:07,190
So that's the code to download any files from the internet.

49
00:04:07,460 --> 00:04:08,630
Thank you, I'll talk to you later.

