JECSand / yahoofinancials

A powerful financial data module used for pulling data from Yahoo Finance. This module can pull fundamental and technical data for stocks, indexes, currencies, cryptos, ETFs, Mutual Funds, U.S. Treasuries, and commodity futures.
https://pypi.python.org/pypi/yahoofinancials
MIT License
896 stars 214 forks source link

TypeError: can only concatenate str (not "NoneType") to str #159

Closed pzarfos closed 7 months ago

pzarfos commented 8 months ago

Here is a short Python script that demonstrates the issue:

from yahoofinancials import YahooFinancials

ticker = "AAPL"
data = YahooFinancials(ticker)

ticker = data.ticker
price = data.get_current_price()

print(f"Ticker: {ticker}")
print(f"Price:  {price}")

When running that script, I got the following error, using yahoofinancials 1.17:

Traceback (most recent call last):
  File "./example.py", line 8, in <module>
    price = data.get_current_price()
  File "./env/lib/python3.9/site-packages/yahoofinancials/yf.py", line 226, in get_current_price
    return self._stock_price_data('regularMarketPrice')
  File "./env/lib/python3.9/site-packages/yahoofinancials/yf.py", line 162, in _stock_price_data
    price_data = self.get_stock_price_data()
  File "./env/lib/python3.9/site-packages/yahoofinancials/yf.py", line 99, in get_stock_price_data
    return self.get_clean_data(self.get_stock_tech_data('price'), 'price')
  File "./env/lib/python3.9/site-packages/yahoofinancials/etl.py", line 595, in get_stock_tech_data
    return self.get_stock_data(tech_type=tech_type)
  File "./env/lib/python3.9/site-packages/yahoofinancials/etl.py", line 565, in get_stock_data
    dict_ent = self._create_dict_ent(self.ticker, statement_type, tech_type, report_name, hist_obj)
  File "./env/lib/python3.9/site-packages/yahoofinancials/etl.py", line 521, in _create_dict_ent
    re_data = self._get_historical_data(YAHOO_URL, r_map, tech_type, statement_type)
  File "./env/lib/python3.9/site-packages/yahoofinancials/etl.py", line 248, in _get_historical_data
    self._request_handler(url, config.get("response_field"))
  File "./env/lib/python3.9/site-packages/yahoofinancials/etl.py", line 190, in _request_handler
    cur_url += "&crumb=" + self.crumb
TypeError: can only concatenate str (not "NoneType") to str

The installed PIP packages are:

# pip freeze
beautifulsoup4==4.11.1
black==23.10.1
certifi==2022.12.7
cffi==1.15.1
charset-normalizer==3.0.1
click==8.1.7
cryptography==39.0.0
cycler==0.11.0
fonttools==4.33.3
idna==3.4
kiwisolver==1.4.2
matplotlib==3.5.2
mypy-extensions==1.0.0
numpy==1.22.3
packaging==23.2
pandas==1.4.2
pathspec==0.11.2
Pillow==9.1.0
platformdirs==3.11.0
pycparser==2.21
pycryptodome==3.18.0
pyparsing==3.0.9
python-dateutil==2.8.2
pytz==2022.1
requests==2.28.2
six==1.16.0
soupsieve==2.3.2.post1
tabulate==0.9.0
termcolor==1.1.0
tomli==2.0.1
typing-extensions==4.8.0
urllib3==1.26.14
yahoofinancials==1.17

I am running Python 3.9.2 in a virtual env on Linux.

It is possible this is related to issue https://github.com/JECSand/yahoofinancials/issues/157

JECSand commented 8 months ago

I’ll take a look at this tonight.

pzarfos commented 7 months ago

OK, this is embarrassing, my ad blocker was causing the problem.

I am using pi-hole for my ad blocker. I swear it was working last month though. They must have recently added yahoo domain names to their ad-block database.

The solution is to make sure you have the yahoo finance domain names whitelisted in your ad-blocker:

fc.yahoo.com
finance.yahoo.com
query2.finance.yahoo.com