urllib.error.HTTPError: HTTP Error 404: Not Found yfinance library

urllib.error.HTTPError: HTTP Error 404: Not Found yfinance library

我在使用时不断收到此错误:

将 yfinance 导入为 yf

msft = yf.Ticker("MSFT")

msft.info

Traceback (most recent call last):
  Python Shell, prompt 15, line 6
  File "C:\Users\i\miniconda3\Lib\site-packages\yfinance\ticker.py", line 138, in info
    return self.get_info()
  File "C:\Users\i\miniconda3\Lib\site-packages\yfinance\base.py", line 415, in get_info
    self._get_fundamentals(proxy)
  File "C:\Users\i\miniconda3\Lib\site-packages\yfinance\base.py", line 284, in _get_fundamentals
    holders = _pd.read_html(url)
  File "C:\Users\i\miniconda3\Lib\site-packages\pandas\io\html.py", line 1100, in read_html
    displayed_only=displayed_only,
  File "C:\Users\i\miniconda3\Lib\site-packages\pandas\io\html.py", line 895, in _parse
    tables = p.parse_tables()
  File "C:\Users\i\miniconda3\Lib\site-packages\pandas\io\html.py", line 213, in parse_tables
    tables = self._parse_tables(self._build_doc(), self.match, self.attrs)
  File "C:\Users\i\miniconda3\Lib\site-packages\pandas\io\html.py", line 733, in _build_doc
    raise e
  File "C:\Users\i\miniconda3\Lib\site-packages\pandas\io\html.py", line 714, in _build_doc
    with urlopen(self.io) as f:
  File "C:\Users\i\miniconda3\Lib\site-packages\pandas\io\common.py", line 141, in urlopen
    return urllib.request.urlopen(*args, **kwargs)
  File "C:\Users\i\miniconda3\Lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\i\miniconda3\Lib\urllib\request.py", line 531, in open
    response = meth(req, response)
  File "C:\Users\i\miniconda3\Lib\urllib\request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Users\i\miniconda3\Lib\urllib\request.py", line 569, in error
    return self._call_chain(*args)
  File "C:\Users\i\miniconda3\Lib\urllib\request.py", line 503, in _call_chain
    result = func(*args)
  File "C:\Users\i\miniconda3\Lib\urllib\request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found

我已尝试更新软件包并卸载并重新安装库,但似乎没有任何效果。

根据this ticker, it's a bug in yfinance图书馆。

让我看看评论:

  • 一个人 fixed 用 monkey-patching 库更改 base.py:
holders = _pd.read_html(url)

与:

r = _requests.get(url,headers={'User-Agent': 'Mozilla/5.0'})
holders = _pd.read_html(r.content)
pip install -i https://pypi.anaconda.org/ranaroussi/simple yfinance

测试了最后一个,有效。祝你好运。