Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: stocks.get_stock_financial_summary raises ValueError and lxml.etree.ParserError #444

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions investpy/stocks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2018-2021 Alvaro Bartolome, alvarobartt @ GitHub
# See LICENSE for details.

from datetime import datetime, date, timedelta
from datetime import datetime, timedelta
import pytz

from random import randint
Expand All @@ -20,7 +20,6 @@
from .utils.data import Data

from .data.stocks_data import stocks_as_df, stocks_as_list, stocks_as_dict
from .data.stocks_data import stock_countries_as_list


def get_stocks(country=None):
Expand Down Expand Up @@ -1428,6 +1427,9 @@ def get_stock_financial_summary(stock, country, summary_type='income_statement',
if req.status_code != 200:
raise ConnectionError("ERR#0015: error " + str(req.status_code) + ", try again later.")

if not req.text:
raise RuntimeError('ERR#0139: no matching financial summary found.')

root = fromstring(req.text)
tables = root.xpath(".//div[@class='companySummaryIncomeStatement']\
/table[contains(@class, 'companyFinancialSummaryTbl')]")
Expand All @@ -1449,7 +1451,12 @@ def get_stock_financial_summary(stock, country, summary_type='income_statement',
curr_row = row.text_content().strip()
data[curr_row] = list()
continue
data[curr_row].append(float(row.text_content().strip()))

curr_value = row.text_content().strip()
if curr_value:
data[curr_row].append(float(curr_value))
else:
data[curr_row].append('N/A')

dataset = pd.DataFrame(data)
dataset.set_index('Date', inplace=True)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_investpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ def test_investpy_stocks():
'country': 'united kingdom',
'summary_type': 'cash_flow_statement',
'period': 'annual'
},
{
'stock': 'blng',
'country': 'russia',
'summary_type': 'cash_flow_statement',
'period': 'quarterly'
}
]

Expand Down