In this tutorial, we will be creating a simple Python script to download files from the web.
Buy Me a Coffee? Your support is much appreciated!
PayPal Me: https://www.paypal.me/jiejenn/5
Venmo: @Jie-Jenn
Source Code:
import requests
downloadUrl = 'https://omextemplates.content.office.net/support/templates/en-us/tf16402488.dotx'
req = requests.get(downloadUrl)
filename = req.url[downloadUrl.rfind('/')+1:]
with open(filename, 'wb') as f:
for chunk in req.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
def download_file(url, filename=''):
try:
if filename:
pass
else:
filename = req.url[downloadUrl.rfind('/')+1:]
with requests.get(url) as req:
with open(filename, 'wb') as f:
for chunk in req.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
return filename
except Exception as e:
print(e)
return None
downloadLink = 'https://omextemplates.content.office.net/support/templates/en-us/tf02896572.dotm'
download_file(downloadLink, 'calendar template.dotm')
Thanks bro…
One quick question sir
In the last part the video you define a string download link and copied the link address
Can we just use a input one to add the name
See l am trying to make a subtitle file downloader from a website
So will it work right ?
Yup, that will work.
hi Jiejenn,
What if we pass thru a URL that does not exist or if the file is missing. How do we check to make sure the link/file is valid? How do we stop the script?
Thanks
I want to download an Excel file from a SharePoint link. please help.