In this video, you will learn how to download a file from a website with Python, for this example,
we will download an MP free file from this website.
File samples, dot com slash formats, slash and B3.
You can also find this link in the resources, and I'll download this first file here.
So for that, I need to know the link to the file and you see this button here.
If I press this button, I will download the file through my browser, but I once downloaded through
Python.
So what I do is I want to know the link to the file, so I want to copy the link.
So right click over the download button.
Copy the link and keep it somewhere in your script.
So that is the link pointing to the empty three file, so I'll give you therefore null and import requests.
Request is a third party in the library for Python able to make, get and post requests.
It gets requests is when you access a file or a web page, so if you download the file that is a gets
request.
A post request is when we want to upload the file.
In our case, we want to use a get requests.
So for that, we see requests that get so get this method and it gets as arguments.
The URL, the URL is the URL to the file.
So let me create a variable here you URL and this should be a string.
So I'm using this double quotes there.
So you URL goes here.
And then if you just print a wreck, that content.
And run the script.
What we will have in the Commons line is the voice over the file.
So this is a binary file.
We have text files and we have binary files.
Binary files are files that are not text files such as MP three in this case.
Now what do we want to do with this binary file is we want to open a file using the open function and
to give it a name such as don't load the MP three and we open that in right binary mode.
So we are creating a new empty file with that name.
And with this file handling methods, so W.B. means right binary.
If you want to read the file you'd be using or B, we're talking about binary files again.
So it would be what we want to use as a file and file.
That's right.
So we will write content to this new empty file.
The content is this thing in here, Rick, that content.
So what I'm going to do is I'm I'll create a new variable content.
We just see you to read that content and then I provide that content variable to here.
So I'm going to stop the execution of the previous run because it's too much work for Python to print
it out, to print all these bites.
And I'm not going to print out anything this time.
So I deleted the print function.
I'm just going to run this.
And don't load that empty three is here.
So if you click that's or downloaded.
From here and then play with your major player.
And that's a downloaded file.
So that's the code to download any files from the internet.
Thank you, I'll talk to you later.