使用pandas-datareader时报警告A true SSLContext object is not available...SSL connections to fail

你在这里

使用pandas-datareader时报警告A true SSLContext object is not available...SSL connections to fail

运行以下代码时报多行警告:
import pandas_datareader.data as web 
all_data = {ticker: web.get_data_yahoo(ticker) for ticker in ['AAPL', 'IBM', 'MSFT', 'GOOG']}
price = pd.DataFrame({ticker: data['Adj Close'] for ticker, data in all_data.items()})
volume = pd.DataFrame({ticker: data['Volume'] for ticker, data in all_data.items()})
每段警告信息都如下:
E:\Python27\lib\site-packages\urllib3\util\ssl_.py:137: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecurePlatformWarning
首先,因为要下载的包比较大,因此在命令行加大pipe超时时间:
pip --default-timeout=100 install -U pip
然后,因为SSL的问题,urllib3需要pyopenssl,因此下载并安装相关的包:
pip install pyopenssl ndg-httpsclient pyasn1
之后就可以看到来自雅虎的股票信息了:
 
但要注意哦,如果在yahoo多获取几次数据后,可能会报无法连接错误:
 
-----------------------------------------------------------------
ConnectionError                           Traceback (most recent call last)
C:\Users\zhu\<ipython-input-102-c7637ba73a15> in <module>()
      1 start = datetime.datetime(2016, 1, 1) # or start = '1/1/2016'
      2 end = datetime.date.today()
----> 3 prices = web.DataReader('AAPL', 'yahoo', start, end)
      4 prices.head()

E:\Python27\lib\site-packages\pandas_datareader\data.pyc in DataReader(name, data_source, start, end, retry_count, pause, session, access_key)
    119                                 adjust_price=False, chunksize=25,
    120                                 retry_count=retry_count, pause=pause,
--> 121                                 session=session).read()
    122 
    123     elif data_source == "yahoo-actions":

E:\Python27\lib\site-packages\pandas_datareader\yahoo\daily.pyc in __init__(self, symbols, start, end, retry_count, pause, session, adjust_price, ret_index, chunksize, interval)
     80 
     81         self.interval = '1' + self.interval
---> 82         self.crumb = self._get_crumb(retry_count)
     83 
     84     @property

E:\Python27\lib\site-packages\pandas_datareader\yahoo\daily.pyc in _get_crumb(self, retries)
    156         tu = "https://finance.yahoo.com/quote/{}/history".format(self.symbols)
    157         response = self._get_response(tu,
--> 158                                       params=self.params, headers=self.headers)
    159         out = str(self._sanitize_response(response))
    160         # Matches: {"crumb":"AlphaNumeric"}


E:\Python27\lib\site-packages\pandas_datareader\base.pyc in _get_response(self, url, params, headers)
    124             response = self.session.get(url,
    125                                         params=params,
--> 126                                         headers=headers)
    127             if response.status_code == requests.codes.ok:
    128                 return response

E:\Python27\lib\site-packages\requests\sessions.pyc in get(self, url, **kwargs)
    519 
    520         kwargs.setdefault('allow_redirects', True)
--> 521         return self.request('GET', url, **kwargs)
    522 
    523     def options(self, url, **kwargs):

E:\Python27\lib\site-packages\requests\sessions.pyc in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    506         }
    507         send_kwargs.update(settings)
--> 508         resp = self.send(prep, **send_kwargs)
    509 
    510         return resp

E:\Python27\lib\site-packages\requests\sessions.pyc in send(self, request, **kwargs)
    616 
    617         # Send the request

--> 618         r = adapter.send(request, **kwargs)
    619 
    620         # Total elapsed time of the request (approximately)


E:\Python27\lib\site-packages\requests\adapters.pyc in send(self, request, stream, timeout, verify, cert, proxies)
    488 
    489         except (ProtocolError, socket.error) as err:
--> 490             raise ConnectionError(err, request=request)
    491 
    492         except MaxRetryError as e:

ConnectionError: ('Connection aborted.', error(10054, ''))

这是yahoo的防火墙在起作用,它以为你是攻击者哦!魔鬼笑
 
著作权归作者所有。商业转载请联系本站作者获得授权,非商业转载请注明出处 ZZKOOK

您可能感兴趣的文章

登录以发表评论

评论

言之有物!

 
65
高不帅的头像

拜托zzkook给点入门级的

 
68
张老叔的头像

春风十里,ZZKOOK。

 
76
林世飞的头像

还是自己多学习吧。。

 
70
最美是你的头像

王道

 
75
张老叔的头像