我试图通过以下代码从雅虎获取股票信息。
import pandas_datareader.data as web
import datetime
start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2015, 12, 31)
f = web.DataReader('SNE', 'yahoo', start, end)
print(f)
但是有如下错误.... 请有人帮帮我..
--------------------------------------------------------------------------- gaierror Traceback (most recent call last) ~\Anaconda3\lib\site-packages\requests\packages\urllib3\connection.py in _new_conn(self)
140 conn = connection.create_connection(
--> 141 (self.host, self.port), self.timeout, **extra_kw)
142
~\Anaconda3\lib\site-packages\requests\packages\urllib3\util\connection.py in create_connection(address, timeout, source_address, socket_options)
59
---> 60 for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
61 af, socktype, proto, canonname, sa = res
~\Anaconda3\lib\socket.py in getaddrinfo(host, port, family, type, proto, flags)
742 addrlist = []
--> 743 for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
744 af, socktype, proto, canonname, sa = res
gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
NewConnectionError Traceback (most recent call last) ~\Anaconda3\lib\site-packages\requests\packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
599 body=body, headers=headers,
--> 600 chunked=chunked)
601
~\Anaconda3\lib\site-packages\requests\packages\urllib3\connectionpool.py in _make_request(self, conn, method, url, timeout, chunked,
**httplib_request_kw)
355 else:
--> 356 conn.request(method, url, **httplib_request_kw)
357
~\Anaconda3\lib\http\client.py in request(self, method, url, body, headers, encode_chunked) 1238 """Send a complete request to the server."""
-> 1239 self._send_request(method, url, body, headers, encode_chunked) 1240
~\Anaconda3\lib\http\client.py in _send_request(self, method, url, body, headers, encode_chunked) 1284 body =
_encode(body, 'body')
-> 1285 self.endheaders(body, encode_chunked=encode_chunked) 1286
~\Anaconda3\lib\http\client.py in endheaders(self, message_body, encode_chunked) 1233 raise CannotSendHeader()
-> 1234 self._send_output(message_body, encode_chunked=encode_chunked) 1235
~\Anaconda3\lib\http\client.py in _send_output(self, message_body, encode_chunked) 1025 del self._buffer[:]
-> 1026 self.send(msg) 1027
~\Anaconda3\lib\http\client.py in send(self, data)
963 if self.auto_open:
--> 964 self.connect()
965 else:
~\Anaconda3\lib\site-packages\requests\packages\urllib3\connection.py in connect(self)
165 def connect(self):
--> 166 conn = self._new_conn()
167 self._prepare_conn(conn)
~\Anaconda3\lib\site-packages\requests\packages\urllib3\connection.py in _new_conn(self)
149 raise NewConnectionError(
--> 150 self, "Failed to establish a new connection: %s" % e)
151
NewConnectionError: <requests.packages.urllib3.connection.HTTPConnection object at 0x0000001B3783DC18>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
MaxRetryError Traceback (most recent call last) ~\Anaconda3\lib\site-packages\requests\adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
437 retries=self.max_retries,
--> 438 timeout=timeout
439 )
~\Anaconda3\lib\site-packages\requests\packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
648 retries = retries.increment(method, url, error=e, _pool=self,
--> 649 _stacktrace=sys.exc_info()[2])
650 retries.sleep()
~\Anaconda3\lib\site-packages\requests\packages\urllib3\util\retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
387 if new_retry.is_exhausted():
--> 388 raise MaxRetryError(_pool, url, error or ResponseError(cause))
389
MaxRetryError: HTTPConnectionPool(host='ichart.finance.yahoo.com', port=80): Max retries exceeded with url: /table.csv?s=SNE&a=0&b=1&c=2010&d=11&e=31&f=2015&g=d&ignore=.csv (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x0000001B3783DC18>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))
During handling of the above exception, another exception occurred:
ConnectionError Traceback (most recent call last) <ipython-input-1-7d307edbe1a3> in <module>()
3 start = datetime.datetime(2010, 1, 1)
4 end = datetime.datetime(2015, 12, 31)
----> 5 f = web.DataReader('SNE', 'yahoo', start, end)
6 print(f)
~\Anaconda3\lib\site-packages\pandas_datareader\data.py in DataReader(name, data_source, start, end, retry_count, pause, session, access_key)
115 adjust_price=False, chunksize=25,
116 retry_count=retry_count, pause=pause,
--> 117 session=session).read()
118
119 elif data_source == "yahoo-actions":
~\Anaconda3\lib\site-packages\pandas_datareader\yahoo\daily.py in read(self)
75 def read(self):
76 """ read one data from specified URL """
---> 77 df = super(YahooDailyReader, self).read()
78 if self.ret_index:
79 df['Ret_Index'] = _calc_return_index(df['Adj Close'])
~\Anaconda3\lib\site-packages\pandas_datareader\base.py in read(self)
155 if isinstance(self.symbols, (compat.string_types, int)):
156 df = self._read_one_data(self.url,
--> 157 params=self._get_params(self.symbols))
158 # Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT'])
159 elif isinstance(self.symbols, DataFrame):
~\Anaconda3\lib\site-packages\pandas_datareader\base.py in
_read_one_data(self, url, params)
72 """ read one data from specified URL """
73 if self._format == 'string':
---> 74 out = self._read_url_as_StringIO(url, params=params)
75 elif self._format == 'json':
76 out = self._get_response(url, params=params).json()
~\Anaconda3\lib\site-packages\pandas_datareader\base.py in
_read_url_as_StringIO(self, url, params)
83 Open url (and retry)
84 """
---> 85 response = self._get_response(url, params=params)
86 text = self._sanitize_response(response)
87 out = StringIO()
~\Anaconda3\lib\site-packages\pandas_datareader\base.py in
_get_response(self, url, params)
112 # initial attempt + retry
113 for i in range(self.retry_count + 1):
--> 114 response = self.session.get(url, params=params)
115 if response.status_code == requests.codes.ok:
116 return response
~\Anaconda3\lib\site-packages\requests\sessions.py in get(self, url,
**kwargs)
529
530 kwargs.setdefault('allow_redirects', True)
--> 531 return self.request('GET', url, **kwargs)
532
533 def options(self, url, **kwargs):
~\Anaconda3\lib\site-packages\requests\sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
516 }
517 send_kwargs.update(settings)
--> 518 resp = self.send(prep, **send_kwargs)
519
520 return resp
~\Anaconda3\lib\site-packages\requests\sessions.py in send(self, request, **kwargs)
637
638 # Send the request
--> 639 r = adapter.send(request, **kwargs)
640
641 # Total elapsed time of the request (approximately)
~\Anaconda3\lib\site-packages\requests\adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
500 raise ProxyError(e, request=request)
501
--> 502 raise ConnectionError(e, request=request)
503
504 except ClosedPoolError as e:
ConnectionError: HTTPConnectionPool(host='ichart.finance.yahoo.com', port=80): Max retries exceeded with url: /table.csv?s=SNE&a=0&b=1&c=2010&d=11&e=31&f=2015&g=d&ignore=.csv (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x0000001B3783DC18>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))
答案 0 :(得分:0)
如果您想使用雅虎财务,请使用Yahoo_finance API。
from yahoo_finance import Share
当您想要检索特定共享使用的值时:
Share('<Your Share Ticker Name>')
然后您可以检索历史数据以及
之类的内容Share('Your Share Ticker Name>').get_historical(<start_date>, <end_date>)
希望这有帮助