Ubuntu 19.10

Ubuntu 19.10

首先,我对 Ubuntu 还很陌生,我使用它才 20 个小时

from yahoo_finance import Share
yahoo = Share('YHOO')

但每当我运行它时终端都会显示:

Traceback (most recent call last):
  File "/usr/lib/python3.7/urllib/request.py", line 1317, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/usr/lib/python3.7/http/client.py", line 1252, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.7/http/client.py", line 1298, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.7/http/client.py", line 1247, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.7/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/usr/lib/python3.7/http/client.py", line 966, in send
    self.connect()
  File "/usr/lib/python3.7/http/client.py", line 1414, in connect
    super().connect()
  File "/usr/lib/python3.7/http/client.py", line 938, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "/usr/lib/python3.7/socket.py", line 707, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/lib/python3.7/socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -5] No address associated with hostname

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/sagar/Documents/Python/AlgoTrading.py", line 3, in <module>
    yahoo = Share('YHOO')
  File "/usr/local/lib/python3.7/dist-packages/yahoo_finance/__init__.py", line 178, in __init__
    self.refresh()
  File "/usr/local/lib/python3.7/dist-packages/yahoo_finance/__init__.py", line 142, in refresh
    self.data_set = self._fetch()
  File "/usr/local/lib/python3.7/dist-packages/yahoo_finance/__init__.py", line 181, in _fetch
    data = super(Share, self)._fetch()
  File "/usr/local/lib/python3.7/dist-packages/yahoo_finance/__init__.py", line 134, in _fetch
    data = self._request(query)
  File "/usr/local/lib/python3.7/dist-packages/yahoo_finance/__init__.py", line 118, in _request
    response = yql.YQLQuery().execute(query)
  File "/usr/local/lib/python3.7/dist-packages/yahoo_finance/yql.py", line 61, in execute
    'env': DATATABLES_URL
  File "/usr/lib/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.7/urllib/request.py", line 525, in open
    response = self._open(req, data)
  File "/usr/lib/python3.7/urllib/request.py", line 543, in _open
    '_open', req)
  File "/usr/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.7/urllib/request.py", line 1360, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/usr/lib/python3.7/urllib/request.py", line 1319, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno -5] No address associated with hostname>
sagar@sagar-desktop:~/Documents/Python$ ^C
sagar@sagar-desktop:~/Documents/Python$ /usr/bin/python3 /home/sagar/Documents/Python/AlgoTrading.py
Traceback (most recent call last):
  File "/usr/lib/python3.7/urllib/request.py", line 1317, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/usr/lib/python3.7/http/client.py", line 1252, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.7/http/client.py", line 1298, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.7/http/client.py", line 1247, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.7/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/usr/lib/python3.7/http/client.py", line 966, in send
    self.connect()
  File "/usr/lib/python3.7/http/client.py", line 1414, in connect
    super().connect()
  File "/usr/lib/python3.7/http/client.py", line 938, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "/usr/lib/python3.7/socket.py", line 707, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/lib/python3.7/socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -5] No address associated with hostname

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/sagar/Documents/Python/AlgoTrading.py", line 3, in <module>
    yahoo = Share('YHOO')
  File "/usr/local/lib/python3.7/dist-packages/yahoo_finance/__init__.py", line 178, in __init__
    self.refresh()
  File "/usr/local/lib/python3.7/dist-packages/yahoo_finance/__init__.py", line 142, in refresh
    self.data_set = self._fetch()
  File "/usr/local/lib/python3.7/dist-packages/yahoo_finance/__init__.py", line 181, in _fetch
    data = super(Share, self)._fetch()
  File "/usr/local/lib/python3.7/dist-packages/yahoo_finance/__init__.py", line 134, in _fetch
    data = self._request(query)
  File "/usr/local/lib/python3.7/dist-packages/yahoo_finance/__init__.py", line 118, in _request
    response = yql.YQLQuery().execute(query)
  File "/usr/local/lib/python3.7/dist-packages/yahoo_finance/yql.py", line 61, in execute
    'env': DATATABLES_URL
  File "/usr/lib/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.7/urllib/request.py", line 525, in open
    response = self._open(req, data)
  File "/usr/lib/python3.7/urllib/request.py", line 543, in _open
    '_open', req)
  File "/usr/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.7/urllib/request.py", line 1360, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/usr/lib/python3.7/urllib/request.py", line 1319, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno -5] No address associated with hostname>

我尝试寻找可以修复此问题的方法,但一无所获。

答案1

我遇到了同样的错误。我阅读了代码并在以下文件中找到了罪魁祸首(损坏的 URL):

https://github.com/lukaszbanasiak/yahoo-finance/blob/master/yahoo_finance/yql.py

不幸的是,雅虎似乎停止了这项服务。由于雅虎停止了其 API,雅虎金融 Python 包已损坏。您必须找到另一个包……这是一个潜在的替代方案:https://rapidapi.com/blog/yahoo-finance-api-python/

有关详细信息,请参阅: https://piunikaweb.com/2019/01/07/query-yahooapis-com-and-weather-yahooapis-com-not-working-heres-why/

https://github.com/yql/yql-tables/issues/476

相关内容