alvarobartt / investpy

Financial Data Extraction from Investing.com with Python
https://investpy.readthedocs.io/
MIT License
1.59k stars 374 forks source link

Empty str to float conversion error fix #523

Open Featuredutka opened 2 years ago

Featuredutka commented 2 years ago

Fix for the issue

511

After a couple of experiments I found out that the error was caused by python trying to convert an empty string to the float.

Sample terminal output for 'PG' stock:

0.41
0.41
0.41

Traceback (most recent call last):
  File "/Users/ash/Desktop/stonks/ui.py", line 107, in run
    main.main(self.progresslink)
  File "/Users/ash/Desktop/stonks/main.py", line 51, in main
    stock_info = investpy.stocks.get_stock_dividends(stock, country=country_data[countryiterator])
  File "/Users/ash/opt/anaconda3/lib/python3.9/site-packages/investpy/stocks.py", line 973, in get_stock_dividends
    dividend_value = float(element_.getnext().text_content())
ValueError: could not convert string to float: ''
Abort trap: 6

So, apparantely that blank line was causing all the problems, but not any more with the try/except wrap.

Featuredutka commented 2 years ago

Some checks later I discovered that the problem is not completely gone. It appears there are some stocks with no provided information at all (like PFE or MRK on the screenshot) - not even an empty string. So the try/exception wrap won't work for those.

Console data output for each stock in list: Screen Shot 2022-02-13 at 01 43 36

To fix that another try/except clause was added

Console output after fix: Screen Shot 2022-02-13 at 02 04 17