Posted  by  admin

Download Files From Website Python


Downloading files from the Internet is one of the most common daily tasks to perform on the Web. It is important due to the fact that a lot of successful software allows their users to download files from the Internet. In this tutorial, you will learn how you can download files over HTTP in Python using the requests library. Apr 05, 2021 How to Scrape all PDF Files from a Website In this part, we’ll learn how to download files from a web directory. We’re going to use BeautifulSoup – the best scraping module of Python, as well as the requests module. As usually, we start with installing all the necessary packages and modules.

Download Web In Python

Here I am going to show a simple example on how to download a file from web application using Python Flask. I will download the file which is kept in the server location. The file could be of any type of file, i.e., pdf, text, excel, word etc. In this example there will be a simple UI (user Interface), where a link will be provided to the end users and clicking on the download link a file will be download. The downloaded file will ask you to choose a file location for saving it.

There could be any reason to download a file from server. The reason could be, you want to generate reports from database and download these reports.


Download files from website python

Python 3.8.0 – 3.9.7/, Flask 1.1.1 – 2.0.1 (pip install flask)

Now I will create the web application that will download any kind of file which is kept in a server location.

Project Directory

First step is to create a project root directory under which I will put all the required files for the project.

Let’s say I am going to create a project root directory python-flask-file-download.

I may not mention the project root directory in subsequent sections while creating new files and I will assume that I am talking with respect to the project’s root directory.

Configuring Flask

Create the below script (py is the extension to indicate Python script) where I import the flask module. This file should be created under the project root directory. Notice how I create flask instance.

Configuring URLs

Next I will create script that will define all URIs or Action paths for performing certain operations such as rendering the UI and downloading the file from server.

In the above source code, the root path or endpoint will simply render the UI. This UI contains only one link for downloading a file from the server. Clicking on the link will ask user to save the file in a chosen location.

the endpoint /download is called on clinking upon the link on UI and a file is sent to the client or UI. The file type could be anything. As you see I have tested with four types of files. I hope this example will work with other file types as well.

UI Template

Now create a download.html file and put it under templates directory. templates directory is the standard directory structure of Flask based web application for putting templates or HTMLs or any static resources – js, css, images.

Notice in the above template file I am using Flask EL (Expression language) to evaluate the link endpoint.

Testing the Application

Now navigate to the project root directory from command line tool and execute the command python or simply if your Python is available on class path, your server will start on default port 5000.

If you want to change the port then you can change the line to, where 5001 is the new port.

Home Page

Hitting the URL http://localhost:5000 will show you a page similar to the below image:

Download Files From Website Python Online

Clicking on the download link will ask you to save the file according to your chosen location.


Download Pdfs From Website

Source Code