Home > OS >  How can I download the CSV file from a Bank of England webpage using Python?
How can I download the CSV file from a Bank of England webpage using Python?

Time:01-31

I would like to programmatically download the CSV file of the Official Bank Rate history on this page: https://www.bankofengland.co.uk/boeapps/database/Bank-Rate.asp There is a link labelled CSV that is clicked to download the file. I think clicking the link must run some javaScript that downloads the file as when I copy the link address it goes to: https://www.bankofengland.co.uk/boeapps/database/Bank-Rate.asp#.

CodePudding user response:

Here's one way to get that data as csv:

import pandas as pd

df = pd.read_html('https://www.bankofengland.co.uk/boeapps/database/Bank-Rate.asp')[0]
df.to_csv('boe_rates.csv')
print(df)

EDIT: And here is another way, using Requests & Pandas (still avoiding the overheads of Selenium):

import requests
import pandas as pd

headers = {
    'accept-language': 'en-US,en;q=0.9',
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}

url = 'https://www.bankofengland.co.uk/boeapps/database/Bank-Rate.asp'
df = pd.read_html(str(requests.get(url, headers=headers).text))[0]
df.to_csv('boe_rates.csv')
print(df)

In both scenarios, the result in terminal (also saved as a csv file) is the same:

    Date Changed    Rate
0   15 Dec 22   3.50
1   03 Nov 22   3.00
2   22 Sep 22   2.25
3   04 Aug 22   1.75
4   16 Jun 22   1.25
... ... ...
242 10 Mar 75   10.25
243 17 Feb 75   10.50
244 10 Feb 75   10.75
245 27 Jan 75   11.00
246 20 Jan 75   11.25
247 rows × 2 columns

You can find Pandas documentation here.

  • Related