ranaroussi / yfinance

Download market data from Yahoo! Finance's API
https://aroussi.com/post/python-yahoo-finance
Apache License 2.0
12.42k stars 2.25k forks source link

Failed to get ticker, "Max retries exceeded" #1804

Open JCPvanHooren opened 6 months ago

JCPvanHooren commented 6 months ago

Describe bug

Failed to get ticker 'USDEUR=X' reason: HTTPSConnectionPool(host='fc.yahoo.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000001FDD7874380>: Failed to establish a new connection: [WinError 10049] The requested address is not valid in its context'))  
USDEUR=X: No timezone found, symbol may be delisted

Simple code that reproduces your problem


usd = yfinance.Ticker('USDEUR=X')
usd.info

### Debug log

DEBUG    get_raw_json(): https://query2.finance.yahoo.com/v10/finance/quoteSummary/USDEUR=X
DEBUG    Entering get()
DEBUG     url=https://query2.finance.yahoo.com/v10/finance/quoteSummary/USDEUR=X
DEBUG     params={'modules': 'financialData,quoteType,defaultKeyStatistics,assetProfile,summaryDetail', 'ssl': 'true'}
DEBUG     Entering _get_cookie_and_crumb()
DEBUG      cookie_mode = 'basic'
DEBUG      Entering _get_cookie_and_crumb_basic()
Traceback (most recent call last):
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\urllib3\connection.py", line 203, in _new_conn
    sock = connection.create_connection(
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\urllib3\util\connection.py", line 85, in create_connection
    raise err
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\urllib3\util\connection.py", line 73, in create_connection
    sock.connect(sa)
OSError: [WinError 10049] The requested address is not valid in its context

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\urllib3\connectionpool.py", line 790, in urlopen
    response = self._make_request(
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\urllib3\connectionpool.py", line 491, in _make_request
    raise new_e
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\urllib3\connectionpool.py", line 467, in _make_request
    self._validate_conn(conn)
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\urllib3\connectionpool.py", line 1096, in _validate_conn
    conn.connect()
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\urllib3\connection.py", line 611, in connect
    self.sock = sock = self._new_conn()
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\urllib3\connection.py", line 218, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x000001EDF21411F0>: Failed to establish a new connection: [WinError 10049] The requested address is not valid in its context

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\requests\adapters.py", line 486, in send
    resp = conn.urlopen(
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\urllib3\connectionpool.py", line 844, in urlopen
    retries = retries.increment(
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\urllib3\util\retry.py", line 515, in increment
    raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='fc.yahoo.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000001EDF21411F0>: Failed to establish a new connection: [WinError 10049] The requested address is not valid in its context'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    usd.info
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\yfinance\ticker.py", line 142, in info
    return self.get_info()
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\yfinance\base.py", line 1736, in get_info
    data = self._quote.info
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\yfinance\scrapers\quote.py", line 572, in info
    self._fetch(self.proxy)
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\yfinance\scrapers\quote.py", line 602, in _fetch
    result = self._data.get_raw_json(
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\yfinance\data.py", line 399, in get_raw_json
    response = self.get(url, user_agent_headers=user_agent_headers, params=params, proxy=proxy, timeout=timeout)
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\yfinance\utils.py", line 108, in wrapper
    result = func(*args, **kwargs)
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\yfinance\data.py", line 348, in get
    cookie, crumb, strategy = self._get_cookie_and_crumb()
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\yfinance\utils.py", line 108, in wrapper
    result = func(*args, **kwargs)
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\yfinance\data.py", line 324, in _get_cookie_and_crumb
    cookie, crumb = self._get_cookie_and_crumb_basic(proxy, timeout)
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\yfinance\utils.py", line 108, in wrapper
    result = func(*args, **kwargs)
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\yfinance\data.py", line 213, in _get_cookie_and_crumb_basic
    cookie = self._get_cookie_basic(proxy, timeout)
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\yfinance\data.py", line 162, in _get_cookie_basic
    response = self._session.get(
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\requests\sessions.py", line 602, in get
    return self.request("GET", url, **kwargs)
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\requests\sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\joost\AppData\Roaming\Python\Python312\site-packages\requests\adapters.py", line 519, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='fc.yahoo.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000001EDF21411F0>: Failed to establish a new connection: [WinError 10049] The requested address is not valid in its context'))

### Bad data proof

_No response_

### `yfinance` version

0.2.33

### Python version

3.12.1

### Operating system

Windows
JCPvanHooren commented 6 months ago

Forgot to mention that USDEUR is just an example. I have the same issue for every ticker. Last month everything still worked fine, using the exact same script.

ValueRaider commented 6 months ago

To avoid wasting time, try dev branch #1080

2 steps to fix:

  1. Understand exception
  2. Safely handle exception and trigger switch to other strategy (data.py function _get_cookie_and_crumb)

I don't like Max retries exceeded ..., implies spam. How many times is it retrying? How to change?

JCPvanHooren commented 6 months ago

Don't ask me how or why, but it's working again.

ValueRaider commented 6 months ago

Leave open. This has happened before #1750, yfinance should handle this nicely instead of blowing up.

Ghodot commented 6 months ago

Hi ! I encountered the same exact issue and as suggested in #1750, it was due to my AdGuard instance blocking the fc.yahoo.com domain. The issue was gone once I whitelisted this domain

keskinbu commented 5 months ago

Hi ! I encountered the same exact issue and as suggested in #1750, it was due to my AdGuard instance blocking the fc.yahoo.com domain. The issue was gone once I whitelisted this domain

I'm using PiHole and it was blocking. I added the Yahoo domain to the whitelist. now, it's working. thank you

grigio commented 4 months ago

OMG! I had the same issue with AdGuard, it seems fc.yahoo.com is blocked by adaway.. added an exception for it

yaneshtyagi commented 4 months ago

Hi ! I encountered the same exact issue and as suggested in #1750, it was due to my AdGuard instance blocking the fc.yahoo.com domain. The issue was gone once I whitelisted this domain

Same for me. My hosts file has blocked fc.yahoo.com. Once I commented the line pointing fc.yahoo.com to 0.0.0.0, it worked.

andy47 commented 2 months ago

Same here, added fc.yahoo.com to the whitelist in Pi Hole and working like a dream now.