微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python - 使用 yahoo_fin

如何解决python - 使用 yahoo_fin

我是 Python 新手,我的目标是编写一些用于股票分析的代码。我已经安装了 yahoo_fin 并且刚刚开始尝试使用它。我已经开始使用 get_live_price

提取实时报价 即

>>> get_live_price('aapl')
126.12000274658203

我正在处理以美元计价的股票以及以加元计价的股票,这使我需要以通用货币表示它们。所以我想将所有美元股票转换为加元,反之亦然。当然,在转换货币之前,我需要先确定一只股票的上市货币。 我发现 yahoo_fin.stock_info 包括 get_quote_data 所以例如

>>> get_quote_data('aapl')

产生了这个字典

{'language': 'en-US','region': 'US','quoteType': 'EQUITY','quoteSourceName': 'Nasdaq Real Time Price','triggerable': True,'currency': 'USD','epsTrailingTwelveMonths': 3.687,'epsForward': 4.68,'epsCurrentYear': 4.45,'priceEpsCurrentYear': 28.476248,'sharesOutstanding': 16788100096,'bookValue': 3.936,'fiftyDayAverage': 133.55576,'fiftyDayAverageChange': -6.8364563,'fiftyDayAverageChangePercent': -0.051188033,'twoHundredDayAverage': 122.48235,'twoHundredDayAverageChange': 4.236946,'twoHundredDayAverageChangePercent': 0.034592297,'marketCap': 2127376285696,'forwardPE': 27.076775,'pricetoBook': 32.194942,'sourceInterval': 15,'exchangeDataDelayedBy': 0,'Tradeable': False,'marketState': 'REGULAR','regularMarketChange': -3.1506958,'regularMarketChangePercent': -2.4260383,'regularMarketTime': 1614010862,'regularMarketPrice': 126.7193,'regularMarketDayHigh': 129.72,'regularMarketDayRange': '125.6 - 129.72','regularMarketDayLow': 125.6,'regularMarketVolume': 45892833,'regularMarketPrevIoUsClose': 129.87,'bid': 126.36,'ask': 126.3,'bidSize': 8,'askSize': 8,'fullExchangeName': 'NasdaqGS','financialCurrency': 'USD','regularMarketopen': 128.01,'averageDailyVolume3Month': 104097755,'averageDailyVolume10day': 84604500,'fiftyTwoWeekLowChange': 73.5668,'fiftyTwoWeekLowChangePercent': 1.3840704,'fiftyTwoWeekRange': '53.1525 - 145.09','fiftyTwoWeekHighChange': -18.370697,'fiftyTwoWeekHighChangePercent': -0.12661588,'fiftyTwoWeekLow': 53.1525,'fiftyTwoWeekHigh': 145.09,'dividendDate': 1613001600,'earningsTimestamp': 1611765000,'earningsTimestampStart': 1619607540,'earningsTimestampEnd': 1620043200,'trailingAnnualDividendrate': 0.807,'trailingPE': 34.369217,'trailingAnnualDividendYield': 0.006213906,'firstTradeDateMilliseconds': 345479400000,'priceHint': 2,'exchange': 'NMS','shortName': 'Apple Inc.','longName': 'Apple Inc.','messageBoardId': 'finmb_24937','exchangeTimezoneName': 'America/New_York','exchangeTimezoneshortName': 'EST','gmtOffSetMilliseconds': -18000000,'market': 'us_market','esgPopulated': False,'displayName': 'Apple','symbol': 'AAPL'}

我对字典不熟悉,也不知道如何处理这些数据,但在那一长串数据的早期,我看到了

'货币':'美元'

所以我的问题是,我怎样才能从这样的字典中只获取货币数据或任何其他数据。

解决方法

所以我的问题是,我如何只获取货币数据或任何其他数据 与此相关的数据,来自这样的字典。

存在键值对的字典。在您的特定情况下,您可以通过以下方式访问 'currency':'USD'

data = {'language': 'en-US','region': 'US','quoteType': 'EQUITY','quoteSourceName': 'Nasdaq Real Time Price','triggerable': True,'currency': 'USD','epsTrailingTwelveMonths': 3.687,'epsForward': 4.68,'epsCurrentYear': 4.45,'priceEpsCurrentYear': 28.476248,'sharesOutstanding': 16788100096,'bookValue': 3.936,'fiftyDayAverage': 133.55576,'fiftyDayAverageChange': -6.8364563,'fiftyDayAverageChangePercent': -0.051188033,'twoHundredDayAverage': 122.48235,'twoHundredDayAverageChange': 4.236946,'twoHundredDayAverageChangePercent': 0.034592297,'marketCap': 2127376285696,'forwardPE': 27.076775,'priceToBook': 32.194942,'sourceInterval': 15,'exchangeDataDelayedBy': 0,'tradeable': False,'marketState': 'REGULAR','regularMarketChange': -3.1506958,'regularMarketChangePercent': -2.4260383,'regularMarketTime': 1614010862,'regularMarketPrice': 126.7193,'regularMarketDayHigh': 129.72,'regularMarketDayRange': '125.6 - 129.72','regularMarketDayLow': 125.6,'regularMarketVolume': 45892833,'regularMarketPreviousClose': 129.87,'bid': 126.36,'ask': 126.3,'bidSize': 8,'askSize': 8,'fullExchangeName': 'NasdaqGS','financialCurrency': 'USD','regularMarketOpen': 128.01,'averageDailyVolume3Month': 104097755,'averageDailyVolume10Day': 84604500,'fiftyTwoWeekLowChange': 73.5668,'fiftyTwoWeekLowChangePercent': 1.3840704,'fiftyTwoWeekRange': '53.1525 - 145.09','fiftyTwoWeekHighChange': -18.370697,'fiftyTwoWeekHighChangePercent': -0.12661588,'fiftyTwoWeekLow': 53.1525,'fiftyTwoWeekHigh': 145.09,'dividendDate': 1613001600,'earningsTimestamp': 1611765000,'earningsTimestampStart': 1619607540,'earningsTimestampEnd': 1620043200,'trailingAnnualDividendRate': 0.807,'trailingPE': 34.369217,'trailingAnnualDividendYield': 0.006213906,'firstTradeDateMilliseconds': 345479400000,'priceHint': 2,'exchange': 'NMS','shortName': 'Apple Inc.','longName': 'Apple Inc.','messageBoardId': 'finmb_24937','exchangeTimezoneName': 'America/New_York','exchangeTimezoneShortName': 'EST','gmtOffSetMilliseconds': -18000000,'market': 'us_market','esgPopulated': False,'displayName': 'Apple','symbol': 'AAPL'}
print(data['currency'])

当您从字典 USD 访问键 currency 的值时,输出将为 data


为了进一步阅读字典,我建议:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。