'pyfinance dividends and documentation
I'm trying to use pyfinance to pull data, I have run into issues with the dividends. Below is the code, the error I et is:
    import yfinance as yf
print('Enter Ticker:')
symbol = input()
symbol = yf.Ticker(symbol)
print('Forward PE:')
print(symbol.info['forwardPE'])
print('Dividends:')
info = yf.Ticker(symbol).info
div = info.get('trailingAnnualDividendYield')
print(div)
Does anyone have documentation for pyfinance? What I have been able to find is slim, how can I view the modules/classes/etc
Error from python interpreter:
    Enter Ticker:
c
Forward PE:
8.224477
Dividends:
Traceback (most recent call last):
  File "/home/user/Desktop/test.py", line 10, in <module>
    info = yf.Ticker(symbol).info
  File "/home/user/.local/lib/python3.9/site-packages/yfinance/base.py", line 49, in __init__
    self.ticker = ticker.upper()
AttributeError: 'Ticker' object has no attribute 'upper'
Solution 1:[1]
You assign symbol = yf.Ticker(symbol), so symbol is yfinance.Ticker object now, not a string. And then you call yf.Ticker(symbol).info (which is not needed) that leads to an error. Don't save on variables names.
import yfinance as yf
print('Enter Ticker:')
symbol = input()
s = yf.Ticker(symbol)
print('Forward PE:')
print(s.info['forwardPE'])
print('Dividends:')
div = s.info.get('trailingAnnualDividendYield')
print(div)
And the results:
Enter Ticker:
ibm
Forward PE:
12.553453
Dividends:
0.049131215
Solution 2:[2]
This fixed it:
import yfinance as yf
print('Enter Ticker:')
x = input()
symbol = x
symbol = yf.Ticker(symbol)
info = yf.Ticker(x).info
div = info.get('trailingAnnualDividendYield')
print('Forward PE:')
print(symbol.info['forwardPE'])
print('Dividend:')
print(div)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | Yuri Ginsburg | 
| Solution 2 | Tomerikoo | 
