0

我的代码在运行时突然开始引发错误。前段时间运行良好,并且仍然可以在我大约 6 个月前使用的 PC 上运行(对于相同的代码)。

将问题跟踪到 yfinance 软件包,该软件包仅引发错误:

import yfinance as yf

#Test code w. Apple as ticker

tickerData = yf.Ticker('AAPL')

tickerData.info

我得到的错误是:::

Traceback (most recent call last):
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 696, in urlopen
    self._prepare_proxy(conn)
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 964, in _prepare_proxy
    conn.connect()
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connection.py", line 364, in connect
    conn = self._connect_tls_proxy(hostname, conn)
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connection.py", line 501, in _connect_tls_proxy
    socket = ssl_wrap_socket(
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\ssl_.py", line 449, in ssl_wrap_socket
    ssl_sock = _ssl_wrap_socket_impl(
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\ssl_.py", line 493, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 512, in wrap_socket
    return self.sslsocket_class._create(
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 1070, in _create
    self.do_handshake()
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 1341, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:997)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\adapters.py", line 439, in send
    resp = conn.urlopen(
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 755, in urlopen
    retries = retries.increment(
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\retry.py", line 574, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='finance.yahoo.com', port=443): Max retries exceeded with url: /quote/AAPL (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:997)')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Arttu/Sync/Working On/yfinance_test.py", line 4, in <module>
    tickerData.info
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\site-packages\yfinance\ticker.py", line 143, in info
    return self.get_info()
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\site-packages\yfinance\base.py", line 525, in get_info
    self._get_fundamentals(proxy=proxy)
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\site-packages\yfinance\base.py", line 321, in _get_fundamentals
    data = utils.get_json(ticker_url, proxy, self.session)
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\site-packages\yfinance\utils.py", line 56, in get_json
    html = session.get(url=url, proxies=proxy, headers=user_agent_headers).text
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\Arttu\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\adapters.py", line 514, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='finance.yahoo.com', port=443): Max retries exceeded with url: /quote/AAPL (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:997)')))

知道如何解决(以及为什么突然改变?)谢谢

4

1 回答 1

0

对我来说它正在工作,我认为这可能是版本的问题。请通过以下方式安装最新版本

pip install finance

然后你的代码就可以工作了

import yfinance as yf
tickerData = yf.Ticker('AAPL')
print(tickerData.info)

输出:

{'zip': '95014', 'sector': 'Technology', 'fullTimeEmployees': 154000, 'longBusinessSummary': 'Apple Inc. designs, manufactures, and markets smartphones, personal computers, tablets, wearables, and accessories worldwide. It also sells various related services. In addition, the company offers iPhone, a line of smartphones; Mac, a line of personal computers; iPad, a line of multi-purpose tablets; AirPods Max, an over-ear wireless headphone; and wearables, home, and accessories comprising AirPods, Apple TV, Apple Watch, Beats products, HomePod, and iPod touch. Further, it provides AppleCare support services; cloud services store services; and operates various platforms, including the App Store that allow customers to discover and download applications and digital content, such as books, music, video, games, and podcasts. Additionally, the company offers various services, such as Apple Arcade, a game subscription service; Apple Music, which offers users a curated listening experience with on-demand radio stations; Apple News+, a subscription news and magazine service; Apple TV+, which offers exclusive original content; Apple Card, a co-branded credit card; and Apple Pay, a cashless payment service, as well as licenses its intellectual property. The company serves consumers, and small and mid-sized businesses; and the education, enterprise, and government markets. It distributes third-party applications for its products through the App Store. The company also sells its products through its retail and online stores, and direct sales force; and third-party cellular network carriers, wholesalers, retailers, and resellers. Apple Inc. was incorporated in 1977 and is headquartered in Cupertino, California.', 'city': 'Cupertino', 'phone': '408 996 1010', 'state': 'CA', 'country': 'United States', 'companyOfficers': [], 'website': 'https://www.apple.com', 'maxAge': 1, 'address1': 'One Apple Park Way', 'industry': 'Consumer Electronics', 'ebitdaMargins': 0.32867, 'profitMargins': 0.25882, 'grossMargins': 0.41779, 'operatingCashflow': 104037998592, 'revenueGrowth': 0.288, 'operatingMargins': 0.29782, 'ebitda': 120233000960, 'targetLowPrice': 128.01, 'recommendationKey': 'buy', 'grossProfits': 152836000000, 'freeCashflow': 73295003648, 'targetMedianPrice': 170, 'currentPrice': 179.45, 'earningsGrowth': 0.662, 'currentRatio': 1.075, 'returnOnAssets': 0.20179, 'numberOfAnalystOpinions': 41, 'targetMeanPrice': 170.78, 'debtToEquity': 216.392, 'returnOnEquity': 1.47443, 'targetHighPrice': 200, 'totalCash': 62639001600, 'totalDebt': 136521998336, 'totalRevenue': 365817004032, 'totalCashPerShare': 3.818, 'financialCurrency': 'USD', 'revenuePerShare': 21.904, 'quickRatio': 0.91, 'recommendationMean': 1.9, 'exchange': 'NMS', 'shortName': 'Apple Inc.', 'longName': 'Apple Inc.', 'exchangeTimezoneName': 'America/New_York', 'exchangeTimezoneShortName': 'EST', 'isEsgPopulated': False, 'gmtOffSetMilliseconds': '-18000000', 'quoteType': 'EQUITY', 'symbol': 'AAPL', 'messageBoardId': 'finmb_24937', 'market': 'us_market', 'annualHoldingsTurnover': None, 'enterpriseToRevenue': 8.25, 'beta3Year': None, 'enterpriseToEbitda': 25.101, '52WeekChange': 0.4735589, 'morningStarRiskRating': None, 'forwardEps': 6.15, 'revenueQuarterlyGrowth': None, 'sharesOutstanding': 16406400000, 'fundInceptionDate': None, 'annualReportExpenseRatio': None, 'totalAssets': None, 'bookValue': 3.841, 'sharesShort': 112598907, 'sharesPercentSharesOut': 0.0069, 'fundFamily': None, 'lastFiscalYearEnd': 1632528000, 'heldPercentInstitutions': 0.58814, 'netIncomeToCommon': 94679998464, 'trailingEps': 5.61, 'lastDividendValue': 0.22, 'SandP52WeekChange': 0.2918527, 'priceToBook': 46.7196, 'heldPercentInsiders': 0.00071000005, 'nextFiscalYearEnd': 1695600000, 'yield': None, 'mostRecentQuarter': 1632528000, 'shortRatio': 1.4, 'sharesShortPreviousMonthDate': 1635465600, 'floatShares': 16389334347, 'beta': 1.203116, 'enterpriseValue': 3018010918912, 'priceHint': 2, 'threeYearAverageReturn': None, 'lastSplitDate': 1598832000, 'lastSplitFactor': '4:1', 'legalType': None, 'lastDividendDate': 1636070400, 'morningStarOverallRating': None, 'earningsQuarterlyGrowth': 0.622, 'priceToSalesTrailing12Months': 8.048091, 'dateShortInterest': 1638230400, 'pegRatio': 1.99, 'ytdReturn': None, 'forwardPE': 29.178862, 'lastCapGain': None, 'shortPercentOfFloat': 0.0069, 'sharesShortPriorMonth': 100495541, 'impliedSharesOutstanding': None, 'category': None, 'fiveYearAverageReturn': None, 'previousClose': 174.56, 'regularMarketOpen': 175.205, 'twoHundredDayAverage': 140.638, 'trailingAnnualDividendYield': 0.004869386, 'payoutRatio': 0.1515, 'volume24Hr': None, 'regularMarketDayHigh': 179.63, 'navPrice': None, 'averageDailyVolume10Day': 123402715, 'regularMarketPreviousClose': 174.56, 'fiftyDayAverage': 153.4292, 'trailingAnnualDividendRate': 0.85, 'open': 175.205, 'toCurrency': None, 'averageVolume10days': 123402715, 'expireDate': None, 'algorithm': None, 'dividendRate': 0.88, 'exDividendDate': 1636070400, 'circulatingSupply': None, 'startDate': None, 'regularMarketDayLow': 174.7, 'currency': 'USD', 'trailingPE': 31.98752, 'regularMarketVolume': 115402748, 'lastMarket': None, 'maxSupply': None, 'openInterest': None, 'marketCap': 2944128516096, 'volumeAllCurrencies': None, 'strikePrice': None, 'averageVolume': 85057687, 'dayLow': 174.7, 'ask': 181.2, 'askSize': 1800, 'volume': 115402748, 'fiftyTwoWeekHigh': 179.63, 'fromCurrency': None, 'fiveYearAvgDividendYield': 1.2, 'fiftyTwoWeekLow': 116.21, 'bid': 181.2, 'tradeable': False, 'dividendYield': 0.0049, 'bidSize': 1000, 'dayHigh': 179.63, 'regularMarketPrice': 179.45, 'preMarketPrice': 181.44, 'logo_url': 'https://logo.clearbit.com/apple.com'}
于 2021-12-13T11:20:52.097 回答