

Progress = tqdm(er_content(buffer_size), f"Downloading ", total=file_size, unit="B", unit_scale=True, unit_divisor=1024) Let's download the file now: # progress bar, changing the unit to bytes instead of iteration (default by tqdm) We get the file size in bytes from Content-Length response header, we also get the file name in Content-Disposition header, but we need to parse it using cgi.parse_header() function. # if content dispotion is not available, just use default from URL # extract filename from content dispositionįilename = params.get("filename", default_filename) Value, params = cgi.parse_header(content_disposition) Before we see it in action, we first need to retrieve the total file size and the file name: # get the total file sizeįile_size = int(("Content-Length", 0))Ĭontent_disposition = ("Content-Disposition") Now only the response headers are downloaded and the connection remains open, hence allowing us to control the workflow by the use of iter_content() method. Response = requests.get(url, stream=True) # download the body of response by chunk, not immediately Luckily for us, there is an attribute we can set to True, which is stream parameter: # read 1024 bytes every time


Now the method we gonna use to download content from the web is requests.get(), but the problem is it downloads the file immediately and we don't want that, as it will get stuck on large files and the memory will be filled. We'll be getting the file URL from the command line arguments: # the url of file you want to download, passed from command line arguments Open up a new Python file and import: from tqdm import tqdm We gonna use the tqdm module here just to print a good-looking progress bar in the downloading process. Let's get started, installing the required dependencies: pip3 install requests tqdm Related: How to Use Hash Algorithms in Python using hashlib. In this tutorial, you will learn how you can download files over HTTP in Python using the requests library. It is important due to the fact that a lot of successful software allows their users to download files from the Internet. Disclosure: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.ĭownloading files from the Internet is one of the most common daily tasks to perform on the Web.
