The Python requests module

How to use requests module to download files from sites.
Python
Author
Affiliation

Special Programme in Science (SPS) & Department of Physics, National University of Singapore.

Python’s Requests Magic

The requests module is a powerful library that allows you to communicate with websites and web servers. For example, you can download files programmatically, which you would otherwise have to do manually by pressing a button or two.

Downloading files programmatically

import requests
url = '?'
local_filename = '?'

response = requests.get(url)

with open(local_filename, 'wb') as file:
    file.write(response.content)

base_url="https://www.asc.ohio-state.edu/gan.1/teaching/winter10/"

for i in range(1,40):
    filename = f'Chapter{i}.pdf'             
    file_url = f'{base_url}/{filename}'
    response = requests.get(file_url)

    with open(filename,'wb') as file:
        file.write(response.content)

Some things to note:

  • You must look at the file url to extract the ‘base url’.
  • You must look at the file url to determine the naming convention used for the filename.
  • I did not know how many files there were. So, I used 40.