I am looking to get stock and dividend data for a list of stocks in python. This is what I have but am not getting any data.
import requests
from bs4 import BeautifulSoup
def get_stock_price(stock):
url = f'https://finance.yahoo.com/quote/{stock}/'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
price_element = soup.find('div', {'class': 'My(6px) Pos(r) smartphone_Mt(6px)'})
if price_element:
price = price_element.find('span').text
return float(price.replace(',', ''))
return None
def get_dividend_data(stock):
url = f'https://finance.yahoo.com/quote/{stock}/dividends?p={stock}'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
dividend_element = soup.find('td', {'class': 'Ta(end) Fw(600) Lh(14px)'})
if dividend_element:
dividend = dividend_element.text
return float(dividend.replace(',', ''))
return None
stocks = ['MSFT', 'LTC', 'AGNC', 'GLAD', 'HRZN', 'ARCC', 'HASI', 'SBR']
for stock in stocks:
price = get_stock_price(stock)
dividend = get_dividend_data(stock)
print(f"Stock: {stock}, Price: {price}, Dividend: {dividend}")
that gives me
Stock: MSFT, Price: None, Dividend: None Stock: LTC, Price: None, Dividend: None Stock: AGNC, Price: None, Dividend: None Stock: GLAD, Price: None, Dividend: None Stock: HRZN, Price: None, Dividend: None Stock: ARCC, Price: None, Dividend: None Stock: HASI, Price: None, Dividend: None Stock: SBR, Price: None, Dividend: None
I had also used
import pandas_datareader as pdr
import yfinance as yf
formatted_monthly_dividend = {}
stock_data = {}
ticker_symbols_monthly = ['MSFT', 'LTC','AGNC', 'GLAD', 'HRZN', 'ARCC', 'HASI', 'SBR']
def get_stock_data():
for ticker_symbol in ticker_symbols_monthly:
# Get the stock data for the current ticker symbol
stock = yf.Ticker(ticker_symbol)
# Get the stock info
info = stock.info
if info is None:
print(f"{ticker_symbol} not found!")
else:
# Get the current stock price
price = info.get('regularMarketPrice', 0)
# Get the dividend yield
dividend_yield = info.get('dividendYield', 0)
# Get the market capitalization
market_cap = info.get('marketCap', 0)
# Print the stock data
print(f'Stock for {ticker_symbol}')
print(f'Stock price: {price}')
print(f'Dividend yield: {dividend_yield}')
print(f'Market capitalization: {market_cap}')
print("\n")
for ticker_symbol in ticker_symbols_monthly:
stock = yf.Ticker(ticker_symbol)
info = stock.info
try:
# Get the current stock price
price = info['regularMarketPrice']
# Get the dividend yield
dividend_yield = info['dividendYield']
annual_dividend = dividend_yield * price
if dividend_yield is None:
annual_dividend = 0
else:
annual_dividend = dividend_yield * price
# Add the stock data to the dictionary
stock_data[ticker_symbol] = (price, annual_dividend)
except KeyError:
print(f"{ticker_symbol} not found!")
and this gives me
Stock for MSFT Stock price: 0 Dividend yield: 0.0085 Market capitalization: 2385119281152
Stock for LTC Stock price: 0 Dividend yield: 0.070199996 Market capitalization: 1335027456
Stock for AGNC Stock price: 0 Dividend yield: 0.1552 Market capitalization: 5474593280
Stock for GLAD Stock price: 0 Dividend yield: 0.1015 Market capitalization: 350961664
Stock for HRZN Stock price: 0 Dividend yield: 0.1031 Market capitalization: 366804480
Stock for ARCC Stock price: 0 Dividend yield: 0.1033 Market capitalization: 10246345728
Stock for HASI Stock price: 0 Dividend yield: 0.060500003 Market capitalization: 2458731776
Stock for SBR Stock price: 0 Dividend yield: 0.1172 Market capitalization: 1067059008
MSFT not found! LTC not found! AGNC not found! GLAD not found! HRZN not found! ARCC not found! HASI not found! SBR not found!
I don't know what I am doing and most of this code came from bard. I have tried a few other ways and it just doesn't seem to work. Any help would be appreciated. Thanks and have a good one.