yf.Tickers from yfinance to download information for multiple tickers and dynamically access each of them

末鹿安然 提交于 2021-01-07 02:42:35

问题


My question is how to dynamically access each ticker when using yf.Tickers from yfinance in Python?

For example, I have a list of tickers: ['AAPL', 'MSFT', 'AMD'] and use the following code to download thru yfinance:

import yfinance as yf
tickers = yf.Tickers('AAPL MSFT AMD')
tickers.AAPL.info
div = tickers.AAPL.info['trailingAnnualDividendYield']

Now I have to type in each ticker like this: tickers.AAPL.info. Does anyone know how I can access each ticker dynamically?

Thank you!


回答1:


You could try the following:

import yfinance as yf

stocks = ['AAPL', 'MSFT', 'AMD']

for stock in stocks:
    info = yf.Ticker(stock).info
    div = info.get('trailingAnnualDividendYield')
    print(stock, div)

The output is:

AAPL 0.013894105
MSFT 0.013502605
AMD None



回答2:


According to yfinance docs:

Fetching data for multiple tickers:

import yfinance as yf
data = yf.download("SPY AAPL", start="2017-01-01", end="2017-04-30")


来源:https://stackoverflow.com/questions/57317760/yf-tickers-from-yfinance-to-download-information-for-multiple-tickers-and-dynami

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!