fixes; another function implemented

This commit is contained in:
2021-11-27 15:11:42 +01:00
parent 25be101cf9
commit f2abd8f2ce
2 changed files with 28 additions and 17 deletions

View File

@ -1,5 +1,5 @@
import logging
from typing import Union
import re
from aiohttp.client import ClientSession
from bs4 import BeautifulSoup
@ -8,6 +8,8 @@ from bs4.element import ResultSet, Tag
from . import constants
log = logging.getLogger(__name__)
# The resulting dictionary's keys correspond to the name of the item (row) in the financial statement,
# while its values will always be tuples with a length corresponding to the number of periods (columns)
# and elements being the actual numbers, with the exception of the first key-value-pair, which will represent
@ -37,21 +39,21 @@ def is_relevant_table_row(tr: Tag) -> bool:
"""
Returns True if the item in the table row is marked as relevant. Additionally warns when an item is unknown.
"""
item_name = str(tr.find_next('td').find_next('div').string).strip()
if constants.FINANCIAL_STATEMENT_ITEMS[item_name]:
return True
if tr.name != 'tr':
return False
item_name = str(tr.td.div.string).strip()
try:
return constants.FINANCIAL_STATEMENT_ITEMS[item_name]
except KeyError:
log.warning(f"Unknown item name '{item_name}' found in financial statement.")
return False
def find_relevant_table_rows(soup: BeautifulSoup) -> ResultSet:
"""
Returns the table rows containing the data of interest.
"""
table_div = soup.find('div', attrs={'class': 'financials'})
rows = table_div.find_all('tr')[1:-1]
for r in rows:
if not is_relevant_table_row(r):
rows.remove(r)
return rows
return soup.find('div', attrs={'class': 'financials'}).tbody.find_all(is_relevant_table_row)
def extract_row_data(tr: Tag) -> tuple[str, tuple[int]]: