Skip to content

Instantly share code, notes, and snippets.

@colobas
Created November 27, 2023 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colobas/004f2d7a4f6530f261c9118ff7786b2c to your computer and use it in GitHub Desktop.
Save colobas/004f2d7a4f6530f261c9118ff7786b2c to your computer and use it in GitHub Desktop.
import pandas as pd
import requests
from lxml import html, etree
from datetime import datetime as date
from fire import Fire
base_url = 'http://www.euribor-rates.eu/en/euribor-rates-by-year/{}/'
def shorten_label(label):
label = label.replace(
' ', ''
).replace(
'weeks',
'w'
).replace(
'week',
'w'
).replace(
'months',
'm'
).replace(
'month',
'm'
)
return label
def shorten_labels(labels):
return list(map(lambda x: shorten_label(x), labels))
def get_history_data(min_year=1999, max_year=date.now().year):
years = [str(year) for year in range(min_year, max_year + 1)]
tables = []
for year in years:
print(year)
page = requests.get(base_url.format(year))
tree = html.fromstring(page.text)
table = tree.xpath('//table')[0]
table = pd.read_html(etree.tostring(table))[0]
table.columns = ['Date'] + shorten_labels(table.columns[1:])
table["Date"] = pd.to_datetime(table["Date"], dayfirst=False, yearfirst=False)
for col in table.columns[1:]:
table[col] = table[col].str.replace("%", "").astype(float)
tables.append(table)
table = pd.concat(tables, axis=0)
table = table.sort_values("Date").reset_index(drop=True)
table.to_csv(f"euribor-{min_year}-{max_year}.csv", index=False)
if __name__ == '__main__':
Fire(get_history_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment