|
| 1 | +# How to Download Files From URLs With Python |
| 2 | + |
| 3 | +This folder contains sample code for the [How to Download Files From URLs With Python](https://realpython.com/python-download-file-from-url/) tutorial on Real Python. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Some of the code requires the following third-party libraries: |
| 8 | + |
| 9 | +- [`aiohttp`](https://pypi.org/project/aiohttp/) |
| 10 | +- [`requests`](https://pypi.org/project/requests/) |
| 11 | + |
| 12 | +To install them into a [virtual environment](https://realpython.com/python-virtual-environments-a-primer/), type the following commands: |
| 13 | + |
| 14 | +```shell |
| 15 | +$ python3 -m venv venv/ |
| 16 | +$ source venv/bin/activate |
| 17 | +(venv) $ python -m pip install -r requirements.txt |
| 18 | +``` |
| 19 | + |
| 20 | +## Running |
| 21 | + |
| 22 | +### 01_download_urllib |
| 23 | + |
| 24 | +```shell |
| 25 | +$ python 01_download_urllib.py |
| 26 | +Date Wed, 28 Jun 2023 19:40:57 GMT |
| 27 | +Content-Type application/zip |
| 28 | +Content-Length 128310 |
| 29 | +Connection close |
| 30 | +Set-Cookie api_https.cookieCORS=76a6c6567ab12cea5dac4942d8df71cc; Path=/; SameSite=None; Secure |
| 31 | +Set-Cookie api_https.cookie=76a6c6567ab12cea5dac4942d8df71cc; Path=/ |
| 32 | +Cache-Control public, must-revalidate, max-age=1 |
| 33 | +Expires Wed, 28 Jun 2023 19:40:58 GMT |
| 34 | +Last-Modified Wed, 28 Jun 2023 19:40:57 GMT |
| 35 | +Content-Disposition attachment; filename=API_NY.GDP.MKTP.CD_DS2_en_csv_v2_5551501.zip |
| 36 | +Request-Context appId=cid-v1:da002513-bd8b-4441-9f30-737944134422 |
| 37 | +Downloaded file gdp_by_country.zip |
| 38 | +``` |
| 39 | + |
| 40 | +### 02_download_requests |
| 41 | + |
| 42 | +```shell |
| 43 | +(venv) $ python 02_download_requests.py |
| 44 | +response.url = 'https://api.worldbank.org/v2/en/indicator/NY.GDP.MKTP.CD?downloadformat=csv' |
| 45 | +response.ok = True |
| 46 | +response.status_code = 200 |
| 47 | +Downloaded file gdp_by_country.zip |
| 48 | +``` |
| 49 | + |
| 50 | +### 03_download_streaming |
| 51 | + |
| 52 | +```shell |
| 53 | +(venv) $ python 03_download_streaming.py |
| 54 | +Downloading... |
| 55 | +Downloaded file WDI_CSV.zip |
| 56 | +``` |
| 57 | + |
| 58 | +### 04_download_threading |
| 59 | + |
| 60 | +```shell |
| 61 | +(venv) $ python 04_download_threading.py |
| 62 | +Downloaded file API_SP.POP.TOTL_DS2_en_csv_v2_5551506.zip |
| 63 | +Downloaded file API_EN.POP.DNST_DS2_en_csv_v2_5552158.zip |
| 64 | +Downloaded file API_NY.GDP.MKTP.CD_DS2_en_csv_v2_5551501.zip |
| 65 | +``` |
| 66 | + |
| 67 | +### 05_download_sequential |
| 68 | + |
| 69 | +```shell |
| 70 | +(venv) $ python 05_download_sequential.py |
| 71 | +Downloaded file API_SP.POP.TOTL_DS2_en_csv_v2_5551506.zip |
| 72 | +Downloaded file API_NY.GDP.MKTP.CD_DS2_en_csv_v2_5551501.zip |
| 73 | +Downloaded file API_EN.POP.DNST_DS2_en_csv_v2_5552158.zip |
| 74 | +``` |
| 75 | + |
| 76 | +### 06_download_async |
| 77 | + |
| 78 | +```shell |
| 79 | +(venv) $ python 06_download_async.py |
| 80 | +Downloaded file API_SP.POP.TOTL_DS2_en_csv_v2_5551506.zip |
| 81 | +Downloaded file API_EN.POP.DNST_DS2_en_csv_v2_5552158.zip |
| 82 | +Downloaded file API_NY.GDP.MKTP.CD_DS2_en_csv_v2_5551501.zip |
| 83 | +``` |
0 commit comments