How to create a Python program to download file from the web | Python Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey Houston guys welcome to another Python tutorial video my name is Jay in this lesson I'm going to show you how to write a Python script to download file from the web so I'll be using office template website for demonstration so if you go to templates that offers calm you found this website you can download a variety of Microsoft Office template files so for example we can demo resume and cover letter templates calendar flyers planer and trackers and so on I'm going to use one of the files for demonstration so let's use resumes and cover letters so I want to write a Python program to download let's don't stress me in the first step is to want to grab the download link so I'm going to hover my mouse to the download button and right click and I'll copy the link address I'm going to paste this link to my Python script and I'll save this link to available so I'm going to call this variable down low you're out and I'll be using the request library to perform the file downloading task and for some of you you may need to install the requestors library so you can just type pep install requesters no help the download link and we can now make an API call so to make an API call I'm going to use the request library that gets and we'll pass that you're out and you should this should be download URL now if I simply just round these three lines individually and should let me move the panel to rights I'm here from the request option we can print the headers so from the header we have the content lens that's the the file size the content type and the template type I'm not exactly sure what the counting md5 is about the last modified date and create days and so on which is pretty useful if we need to grab some of the metadata layouts needing to download a file in from the request object and if I just referenced since the figure out methods and that gives you the link that you used to download the file so we need the file name the file name is the last item found my URL and to extract the file name so I need to do some slicing but first I need to locate the last four slash so is the rifle method you know search for the four slash and I'll store the output for vehicle file name in here so this will give us the the index position of the string position of the last four slash and here if we just reference that URL attribute we can basically just slice everything found this point found the letter T to the last string so here this would be plus one won't slice all the way to a loss I'll character and now if I print file name in doubt give us the file name right now have to cross up job in the family we can now go ahead and start download the file in here I'm going to say we open file name and we're going to rise binary and just in case if the file size is super is large like four hundred mega by a hundred megabyte or maybe couple gigabyte file size and want to download the file in chunk so what I can do is I can say for chunk request that array contents I'm going to specify my chunk size also that to 1,800 and I need to be I'm going say if condition if chunk is not empty they want to write a chunk to my file and that's it now if I go to my folder should meet up a little bit bigger and now if I go by to my Python script and basically specify to round the script and the stump now if I go back to my folder and here's the the word Templar if I double click here's the the template file now I want to convert the script to a function so I can reuse this this script I'll lay down if I need to download any files from the web so here I'm going to create a new function I'm going to name this function download file and we're going to have a parameter called your out and the other parameter is called is a file name so in case you form to rename the file they want to have the option to do so in here on to insert if condition to verify the file name so file name is not blank do you know the file name has at least some stringing it otherwise want to extract the file name found the link now can make a request code so crashes that gets you out plus i eq and we open file name and want to write binary and here is for chunk so from this point I can just copy this are staying right here in return filename and that's it oh it's just in case I want to use insert right in the except block to capture in the air oops except right and that's it now let me just ask you this function so I'll save this for machining memory now if I go back to the office template website and this time let me choose a different file so called calendars I'll choose the first calendar and I'll grab the tunnel link Amy I'm going to call the function so download file I'll pass the download URL and we'll link and this time going to rename the file so I'll name this file to something more meaningful Kaunda template da do TM and that's the file extension type now fighters from line 33 and downloading study file here you go now if I rewound the function and that returns the the file the file name and now if we go back to our folder and here's our calendar simpler now let me make sure that the file is not corrupt and here's the the word document so it's a macro template file that means if I go to visual studio or window inside this file there are some macros reading into this file and to enhance the functionality but the Zips I'm going to cover in this lesson and hopefully you guys found the video useful as always see you guys watching I'll see you guys on the next video
Info
Channel: Jie Jenn
Views: 122,313
Rating: undefined out of 5
Keywords:
Id: HDEvWfSk2So
Channel Id: undefined
Length: 9min 19sec (559 seconds)
Published: Mon Jan 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.