Created
November 30, 2021 23:16
-
-
Save Aleksey-Voko/6099357370af150298936fc0a5fbc8cc to your computer and use it in GitHub Desktop.
Download file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Download file. | |
============ | |
""" | |
import logging | |
from pathlib import Path | |
import requests | |
USER_AGENT = ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' | |
'(KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36') | |
HEADERS = {'User-Agent': USER_AGENT} | |
def download_file(url, f_name): | |
""" | |
Download file | |
:param url: file link | |
:param f_name: file name | |
""" | |
logging.basicConfig(level=logging.INFO) | |
Path(f_name).parent.mkdir(parents=True, exist_ok=True) | |
while True: | |
try: | |
logging.info(f'\nFile: {Path(f_name).name}\n') | |
response = requests.get(url, headers=HEADERS) | |
logging.info(f'\nDownload file:\n{url}\n{"=" * 30}\n') | |
with open(Path(f_name), 'wb') as f_out: | |
f_out.write(response.content) | |
break | |
except Exception as e: | |
logging.warning(f'{"#" * 10} Exception {"#" * 10}') | |
logging.warning(f'{e}') | |
logging.warning(f'{"#" * 10} Exception {"#" * 10}\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment