yf.Tickers 从 yfinance 下载多个代码的信息并动态访问每个代码
yf.Tickers from yfinance to download information for multiple tickers and dynamically access each of them
我的问题是在 Python 中使用来自 yfinance 的 yf.Tickers 时如何动态访问每个代码?
例如,我有一个代码列表:['AAPL'、'MSFT'、'AMD'] 并使用以下代码通过 yfinance 下载:
import yfinance as yf
tickers = yf.Tickers('AAPL MSFT AMD')
tickers.AAPL.info
div = tickers.AAPL.info['trailingAnnualDividendYield']
现在我必须像这样输入每个代码:tickers.AAPL.info
。有谁知道我如何动态访问每个代码?
谢谢!
您可以尝试以下方法:
import yfinance as yf
stocks = ['AAPL', 'MSFT', 'AMD']
for stock in stocks:
info = yf.Ticker(stock).info
div = info.get('trailingAnnualDividendYield')
print(stock, div)
输出为:
AAPL 0.013894105
MSFT 0.013502605
AMD None
正在获取多个代码的数据:
import yfinance as yf
data = yf.download("SPY AAPL", start="2017-01-01", end="2017-04-30")
import yfinance as yf
tickers = yf.Tickers('AAPL,MSFT,AMD')
for tick in tickers.tickers:
if tick.info['symbol'] == 'AAPL':
print(tick.info)
我的问题是在 Python 中使用来自 yfinance 的 yf.Tickers 时如何动态访问每个代码?
例如,我有一个代码列表:['AAPL'、'MSFT'、'AMD'] 并使用以下代码通过 yfinance 下载:
import yfinance as yf
tickers = yf.Tickers('AAPL MSFT AMD')
tickers.AAPL.info
div = tickers.AAPL.info['trailingAnnualDividendYield']
现在我必须像这样输入每个代码:tickers.AAPL.info
。有谁知道我如何动态访问每个代码?
谢谢!
您可以尝试以下方法:
import yfinance as yf
stocks = ['AAPL', 'MSFT', 'AMD']
for stock in stocks:
info = yf.Ticker(stock).info
div = info.get('trailingAnnualDividendYield')
print(stock, div)
输出为:
AAPL 0.013894105
MSFT 0.013502605
AMD None
正在获取多个代码的数据:
import yfinance as yf
data = yf.download("SPY AAPL", start="2017-01-01", end="2017-04-30")
import yfinance as yf
tickers = yf.Tickers('AAPL,MSFT,AMD')
for tick in tickers.tickers:
if tick.info['symbol'] == 'AAPL':
print(tick.info)