JECSand / yahoofinancials

A powerful financial data module used for pulling data from Yahoo Finance. This module can pull fundamental and technical data for stocks, indexes, currencies, cryptos, ETFs, Mutual Funds, U.S. Treasuries, and commodity futures.
https://pypi.python.org/pypi/yahoofinancials
MIT License
911 stars 215 forks source link

OSError: [Errno socket error] [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:841) #42

Closed maciey closed 4 years ago

maciey commented 4 years ago

Ive got this error when trying extract data

python demo.py WFC C BAC


Traceback (most recent call last): File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\urllib\request.py", line 1757, in open return getattr(self, name)(url) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\urllib\request.py", line 1967, in open_https return self._open_generic_http(self._https_connection, url, data) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\urllib\request.py", line 1916, in _open_generic_http http_conn.request("GET", selector, headers=headers) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 1239, in request self._send_request(method, url, body, headers, encode_chunked) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 1285, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 1234, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 1026, in _send_output self.send(msg) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 964, in send self.connect() File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 1400, in connect server_hostname=server_hostname) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\ssl.py", line 407, in wrap_socket _context=self, _session=session) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\ssl.py", line 814, in init self.do_handshake() File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\ssl.py", line 1068, in do_handshake self._sslobj.do_handshake() File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\ssl.py", line 689, in do_handshake self._sslobj.do_handshake() ssl.SSLError: [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:841)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "demo.py", line 87, in timeit(defaultapi, ts[0] if 1 == len(ts) else ts) File "demo.py", line 68, in timeit f(*args) File "demo.py", line 28, in defaultapi print(tick.get_summary_data()) File "C:\Users\XXXX\Documents\Projekty\Financials\yahoofinancials\yahoofinancials__init.py", line 624, in get_summary_data return self.get_clean_data(self.get_stock_tech_data('summaryDetail'), 'summaryDetail') File "C:\Users\XXXX\Documents\Projekty\Financials\yahoofinancials\yahoofinancials__init.py", line 491, in get_stock_tech_data return self.get_stock_data(tech_type=tech_type) File "C:\Users\XXXX\Documents\Projekty\Financials\yahoofinancials\yahoofinancials__init.py", line 478, in get_stock_data dict_ent = self._create_dict_ent(tick, statement_type, tech_type, report_name, hist_obj) File "C:\Users\XXXX\Documents\Projekty\Financials\yahoofinancials\yahoofinancials\init__.py", line 406, in _create_dict_ent re_data = self._scrape_data(YAHOO_URL, tech_type, statement_type) File "C:\Users\XXXX\Documents\Projekty\Financials\yahoofinancials\yahoofinancials\init__.py", line 144, in _scrape_data response = urlopener.open(url) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\urllib\request.py", line 1763, in open raise OSError('socket error', msg).with_traceback(sys.exc_info()[2]) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\urllib\request.py", line 1757, in open return getattr(self, name)(url) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\urllib\request.py", line 1967, in open_https return self._open_generic_http(self._https_connection, url, data) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\urllib\request.py", line 1916, in _open_generic_http http_conn.request("GET", selector, headers=headers) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 1239, in request self._send_request(method, url, body, headers, encode_chunked) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 1285, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 1234, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 1026, in _send_output self.send(msg) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 964, in send self.connect() File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 1400, in connect server_hostname=server_hostname) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\ssl.py", line 407, in wrap_socket _context=self, _session=session) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\ssl.py", line 814, in init__ self.do_handshake() File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\ssl.py", line 1068, in do_handshake self._sslobj.do_handshake() File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\ssl.py", line 689, in do_handshake self._sslobj.do_handshake() OSError: [Errno socket error] [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:841)

JECSand commented 4 years ago

Thank you for opening this issue. I was unable to replicate this bug, when I ran:

python demo.py WFC C BAC

My output was:

----------------------------------------------------------------
{'C': {'trailingAnnualDividendYield': 0.026670491, 'regularMarketPreviousClose': 69.74, 'currency': 'USD', 'volumeAllCurrencies': None, 'dividendRate': 2.04, 'fiveYearAvgDividendYield': 1.14, 'maxSupply': None, 'lastMarket': None, 'bid': 70.48, 'regularMarketDayLow': 70.52, 'averageVolume10days': 14266850, 'fiftyTwoWeekLow': 48.42, 'fiftyTwoWeekHigh': 73.08, 'startDate': '-', 'trailingPE': 9.537787, 'regularMarketDayHigh': 71.89, 'expireDate': '-', 'twoHundredDayAverage': 67.69029, 'marketCap': 157090824192, 'circulatingSupply': None, 'dividendYield': 0.0293, 'ask': 72.49, 'open': 70.52, 'fromCurrency': None, 'navPrice': None, 'dayHigh': 71.89, 'dayLow': 70.52, 'fiftyDayAverage': 68.61229, 'volume24Hr': None, 'regularMarketVolume': 13075452, 'maxAge': 1, 'totalAssets': None, 'exDividendDate': '2019-08-02', 'openInterest': None, 'regularMarketOpen': 70.52, 'bidSize': 1200, 'averageVolume': 13618300, 'priceHint': 2, 'payoutRatio': 0.2467, 'forwardPE': 8.569212, 'tradeable': True, 'previousClose': 69.74, 'yield': None, 'askSize': 1800, 'algorithm': None, 'trailingAnnualDividendRate': 1.86, 'priceToSalesTrailing12Months': 2.4135144, 'beta': 1.885348, 'averageDailyVolume10Day': 14266850, 'volume': 13075452, 'ytdReturn': None, 'strikePrice': None}, 'BAC': {'trailingAnnualDividendYield': 0.020757824, 'regularMarketPreviousClose': 30.35, 'currency': 'USD', 'volumeAllCurrencies': None, 'dividendRate': 0.72, 'fiveYearAvgDividendYield': 1.4, 'maxSupply': None, 'lastMarket': None, 'bid': 31.06, 'regularMarketDayLow': 30.63, 'averageVolume10days': 61227333, 'fiftyTwoWeekLow': 22.66, 'fiftyTwoWeekHigh': 31.17, 'startDate': '-', 'trailingPE': 11.433837, 'regularMarketDayHigh': 31.15, 'expireDate': '-', 'twoHundredDayAverage': 28.812101, 'marketCap': 282476511232, 'circulatingSupply': None, 'dividendYield': 0.023699999, 'ask': 31.12, 'open': 30.67, 'fromCurrency': None, 'navPrice': None, 'dayHigh': 31.15, 'dayLow': 30.63, 'fiftyDayAverage': 29.052286, 'volume24Hr': None, 'regularMarketVolume': 60954292, 'maxAge': 1, 'totalAssets': None, 'exDividendDate': '2019-09-05', 'openInterest': None, 'regularMarketOpen': 30.67, 'bidSize': 34100, 'averageVolume': 57740843, 'priceHint': 2, 'payoutRatio': 0.2325, 'forwardPE': 10.444445, 'tradeable': True, 'previousClose': 30.35, 'yield': None, 'askSize': 45100, 'algorithm': None, 'trailingAnnualDividendRate': 0.63, 'priceToSalesTrailing12Months': 3.174821, 'beta': 1.638931, 'averageDailyVolume10Day': 61227333, 'volume': 60954292, 'ytdReturn': None, 'strikePrice': None}, 'WFC': {'trailingAnnualDividendYield': 0.03682209, 'regularMarketPreviousClose': 49.97, 'currency': 'USD', 'volumeAllCurrencies': None, 'dividendRate': 2.04, 'fiveYearAvgDividendYield': 2.94, 'maxSupply': None, 'lastMarket': None, 'bid': 0, 'regularMarketDayLow': 50.175, 'averageVolume10days': 23374333, 'fiftyTwoWeekLow': 43.02, 'fiftyTwoWeekHigh': 55.04, 'startDate': '-', 'trailingPE': 10.856282, 'regularMarketDayHigh': 50.5, 'expireDate': '-', 'twoHundredDayAverage': 47.1229, 'marketCap': 216984059904, 'circulatingSupply': None, 'dividendYield': 0.040799998, 'ask': 51.11, 'open': 50.28, 'fromCurrency': None, 'navPrice': None, 'dayHigh': 50.5, 'dayLow': 50.175, 'fiftyDayAverage': 48.813427, 'volume24Hr': None, 'regularMarketVolume': 19371900, 'maxAge': 1, 'totalAssets': None, 'exDividendDate': '2019-08-08', 'openInterest': None, 'regularMarketOpen': 50.28, 'bidSize': 1300, 'averageVolume': 21195260, 'priceHint': 2, 'payoutRatio': 0.3966, 'forwardPE': 11.6, 'tradeable': True, 'previousClose': 49.97, 'yield': None, 'askSize': 4000, 'algorithm': None, 'trailingAnnualDividendRate': 1.84, 'priceToSalesTrailing12Months': 2.5939207, 'beta': 1.186103, 'averageDailyVolume10Day': 23374333, 'volume': 19371900, 'ytdReturn': None, 'strikePrice': None}}
----------------------------------------------------------------
{'C': {'underlyingExchangeSymbol': None, 'headSymbol': None, 'market': 'us_market', 'underlyingSymbol': None, 'exchange': 'NYQ', 'quoteType': 'EQUITY', 'exchangeTimezoneName': 'America/New_York', 'messageBoardId': 'finmb_391687', 'gmtOffSetMilliseconds': '-14400000', 'uuid': '0d61f837-0cad-3d41-af80-b84d143e1257', 'shortName': 'Citigroup, Inc.', 'longName': 'Citigroup Inc.', 'isEsgPopulated': True, 'symbol': 'C', 'exchangeTimezoneShortName': 'EDT'}, 'BAC': {'underlyingExchangeSymbol': None, 'headSymbol': None, 'market': 'us_market', 'underlyingSymbol': None, 'exchange': 'NYQ', 'quoteType': 'EQUITY', 'exchangeTimezoneName': 'America/New_York', 'messageBoardId': 'finmb_19049', 'gmtOffSetMilliseconds': '-14400000', 'uuid': 'bcf30d7f-4abd-3593-b752-cacd38ff491c', 'shortName': 'Bank of America Corporation', 'longName': 'Bank of America Corporation', 'isEsgPopulated': True, 'symbol': 'BAC', 'exchangeTimezoneShortName': 'EDT'}, 'WFC': {'underlyingExchangeSymbol': None, 'headSymbol': None, 'market': 'us_market', 'underlyingSymbol': None, 'exchange': 'NYQ', 'quoteType': 'EQUITY', 'exchangeTimezoneName': 'America/New_York', 'messageBoardId': 'finmb_292891', 'gmtOffSetMilliseconds': '-14400000', 'uuid': '0bc07f6d-ca07-3490-b750-d6d47f2b33a9', 'shortName': 'Wells Fargo & Company', 'longName': 'Wells Fargo & Company', 'isEsgPopulated': True, 'symbol': 'WFC', 'exchangeTimezoneShortName': 'EDT'}}
----------------------------------------------------------------
{'C': {'regularMarketPreviousClose': 69.74, 'regularMarketSource': 'DELAYED', 'volumeAllCurrencies': None, 'underlyingSymbol': None, 'postMarketTime': '2019-10-21 22:21:37 UTC+0000', 'regularMarketPrice': 71.81, 'preMarketSource': 'DELAYED', 'lastMarket': None, 'postMarketPrice': 71.85, 'regularMarketDayLow': 70.52, 'symbol': 'C', 'marketState': 'PRE', 'currency': 'USD', 'regularMarketDayHigh': 71.89, 'exchangeDataDelayedBy': 0, 'preMarketChange': 0.05000305, 'exchangeName': 'NYSE', 'marketCap': 157090824192, 'circulatingSupply': None, 'preMarketTime': '2019-10-22 07:16:16 UTC+0000', 'fromCurrency': None, 'averageDailyVolume3Month': 13618300, 'shortName': 'Citigroup, Inc.', 'regularMarketTime': '2019-10-21 19:02:43 UTC+0000', 'volume24Hr': None, 'regularMarketVolume': 13075452, 'maxAge': 1, 'exchange': 'NYQ', 'openInterest': None, 'quoteType': 'EQUITY', 'preMarketChangePercent': 0.0006963244, 'longName': 'Citigroup Inc.', 'regularMarketOpen': 70.52, 'currencySymbol': '$', 'regularMarketChangePercent': 0.029681671, 'averageDailyVolume10Day': 14266850, 'priceHint': 2, 'postMarketSource': 'DELAYED', 'postMarketChangePercent': 0.00055703823, 'preMarketPrice': 71.86, 'quoteSourceName': 'Delayed Quote', 'postMarketChange': 0.040000916, 'regularMarketChange': 2.0699997, 'strikePrice': None}, 'BAC': {'regularMarketPreviousClose': 30.35, 'regularMarketSource': 'DELAYED', 'volumeAllCurrencies': None, 'underlyingSymbol': None, 'postMarketTime': '2019-10-21 22:55:58 UTC+0000', 'regularMarketPrice': 31.02, 'preMarketSource': 'DELAYED', 'lastMarket': None, 'postMarketPrice': 31.04, 'regularMarketDayLow': 30.63, 'symbol': 'BAC', 'marketState': 'PRE', 'currency': 'USD', 'regularMarketDayHigh': 31.15, 'exchangeDataDelayedBy': 0, 'preMarketChange': 0.14999962, 'exchangeName': 'NYSE', 'marketCap': 282476511232, 'circulatingSupply': None, 'preMarketTime': '2019-10-22 09:43:44 UTC+0000', 'fromCurrency': None, 'averageDailyVolume3Month': 57740843, 'shortName': 'Bank of America Corporation', 'regularMarketTime': '2019-10-21 19:01:06 UTC+0000', 'volume24Hr': None, 'regularMarketVolume': 60954292, 'maxAge': 1, 'exchange': 'NYQ', 'openInterest': None, 'quoteType': 'EQUITY', 'preMarketChangePercent': 0.0048355777, 'longName': 'Bank of America Corporation', 'regularMarketOpen': 30.67, 'currencySymbol': '$', 'regularMarketChangePercent': 0.022075783, 'averageDailyVolume10Day': 61227333, 'priceHint': 2, 'postMarketSource': 'FREE_REALTIME', 'postMarketChangePercent': 0.00064476003, 'preMarketPrice': 31.17, 'quoteSourceName': 'Delayed Quote', 'postMarketChange': 0.020000458, 'regularMarketChange': 0.6700001, 'strikePrice': None}, 'WFC': {'regularMarketSource': 'DELAYED', 'volume24Hr': None, 'regularMarketVolume': 19371900, 'regularMarketPreviousClose': 49.97, 'currency': 'USD', 'volumeAllCurrencies': None, 'averageDailyVolume3Month': 21195260, 'regularMarketOpen': 50.28, 'openInterest': None, 'postMarketTime': '2019-10-21 22:30:15 UTC+0000', 'quoteType': 'EQUITY', 'regularMarketPrice': 50.46, 'maxAge': 1, 'preMarketSource': 'DELAYED', 'lastMarket': None, 'longName': 'Wells Fargo & Company', 'regularMarketDayLow': 50.175, 'currencySymbol': '$', 'marketState': 'PRE', 'averageDailyVolume10Day': 23374333, 'priceHint': 2, 'regularMarketChangePercent': 0.00980584, 'postMarketSource': 'DELAYED', 'underlyingSymbol': None, 'postMarketChangePercent': -0.0021799565, 'exchangeDataDelayedBy': 0, 'regularMarketDayHigh': 50.5, 'preMarketPrice': None, 'quoteSourceName': 'Delayed Quote', 'preMarketChange': None, 'symbol': 'WFC', 'marketCap': 216984059904, 'circulatingSupply': None, 'postMarketChange': -0.11000061, 'regularMarketChange': 0.48999786, 'fromCurrency': None, 'postMarketPrice': 50.35, 'exchange': 'NYQ', 'shortName': 'Wells Fargo & Company', 'exchangeName': 'NYSE', 'strikePrice': None, 'regularMarketTime': '2019-10-21 19:04:09 UTC+0000'}}
----------------------------------------------------------------
{'C': 71.81, 'BAC': 31.02, 'WFC': 50.46}
----------------------------------------------------------------
{'C': 2.04, 'BAC': 0.72, 'WFC': 2.04}
----------------------------------------------------------------
dict_keys(['https://finance.yahoo.com/quote/WFC/financials?p=WFC', 'https://finance.yahoo.com/quote/C/financials?p=C', 'https://finance.yahoo.com/quote/BAC/financials?p=BAC'])
----------------------------------------------------------------
18.64062738418579 seconds
maciey commented 4 years ago

I think I know where is the problem. Im behind web proxy (port 80) I any any way to run script giving proxy as config param ?

JECSand commented 4 years ago

@maciey

That is not something I have built in unfortunately. Also that error is coming from the Yahoo Finance side. The request they require is https, so you'd have to find some way to communicate the request via ssl on port 443 of your proxy. I am not sure what type of proxy you are using or how it is setup. However, if you are using Nginx or Apache you should also be able to configure your proxy's webserver to proxy pass back with https in the headers as a work around. Otherwise, you maybe able to get around this issue by setting up self-signed ssl certs for your proxy.

Setting up proxy pass with https examples:

https://docs.nginx.com/nginx/admin-guide/security-controls/securing-http-traffic-upstream/

Setting up self-signed certs example:

https://hostadvice.com/how-to/how-to-configure-nginx-to-use-self-signed-ssl-tls-certificate-on-ubuntu-18-04-vps-or-dedicated-server/

maciey commented 4 years ago

Thanks. I'll play with that and will let community know if / how to resolve that

I think I'll check 1st this https://stackoverflow.com/questions/31639742/how-to-pass-all-pythons-traffics-through-a-http-proxy