Don't bother using XML namespaces for ECB data.

This commit is contained in:
Chris Berkhout 2021-05-04 17:25:42 +02:00
parent cce33beead
commit 89cf36c357

View file

@ -1,7 +1,6 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
from decimal import Decimal from decimal import Decimal
from lxml import etree from lxml import etree
from lxml.cssselect import CSSSelector
import requests import requests
@ -92,14 +91,13 @@ class ECB:
data = response.content data = response.content
root = etree.fromstring(data) root = etree.fromstring(data)
ns = {"default": "http://www.ecb.int/vocabulary/2002-08-01/eurofxref"}
all_rows = [] all_rows = []
for day in CSSSelector("default|Cube[time]", ns)(root): for day in root.cssselect("[time]"):
date = day.attrib["time"] date = day.attrib["time"]
# TODO what if it's not found for that day? # TODO what if it's not found for that day?
# (some quotes aren't in the earliest data) # (some quotes aren't in the earliest data)
for row in CSSSelector(f"default|Cube[currency='{quote}']", ns)(day): for row in day.cssselect(f"[currency='{quote}']"):
rate = Decimal(row.attrib["rate"]) rate = Decimal(row.attrib["rate"])
all_rows.insert(0, (date, rate)) all_rows.insert(0, (date, rate))
selected = [ selected = [