Search code examples
pythoncsvdownload

download csv file via a website in pyhton


I want to automatically download via python the csv file provided in this website.

https://www.bankofengland.co.uk/boeapps/database/Bank-Rate.asp

I am trying the usual methods suggested here as well as this script:

base_url = 'https://www.bankofengland.co.uk/boeapps/database/Bank-Rate.asp#'
# 1. Download data
orig_m = (pd.read_csv(f'{base_url}.csv').dropna(how='all'))

Nothing has really worked so far. Can someone please help?


Solution

  • It wont be possible to download a .csv directly from this website. They seem to generate a new link to that file everytime you download it.

    You could open the website via python/selenium and then download the file. Since the data is also available on the site itself a faster solution would be to download the website with python/requests and the parse the site with he help of python/pandas.

    Here is a basic example.

    import requests
    import pandas as pd
    
    response = requests.get("https://www.bankofengland.co.uk/boeapps/database/Bank-Rate.asp#").content
    df_list = pd.read_html(response)
    df = df_list[0]
    print(df)
    df.to_csv('my data.csv')