Search code examples
pythonpandasyahoo-financeyahoo-apipandas-datareader

Can you pull current or past dividends data with pandas using yahoo finance api?


I'm new to using the Yahoo finance API for pulling stock data. I watched a tutorial on youtube on how to get the OHLC data, but they never went over getting dividends. Can you pull dividends data from the Yahoo API using pandas/python?


Solution

  • This will give you what you want, and a whole lot more too!

    import csv
    import requests
    from bs4 import BeautifulSoup
    
    url_base = "https://finviz.com/quote.ashx?t="
    tckr = ['SBUX','MSFT','AAPL']
    url_list = [url_base + s for s in tckr]
    
    with open('C:\\path_where_you_want_to_download_data\\SO.csv', 'a', newline='') as f:
        writer = csv.writer(f)
    
        for url in url_list:
            try:
                fpage = requests.get(url)
                fsoup = BeautifulSoup(fpage.content, 'html.parser')
    
                # write header row
                writer.writerow(map(lambda e : e.text, fsoup.find_all('td', {'class':'snapshot-td2-cp'})))
    
                # write body row
                writer.writerow(map(lambda e : e.text, fsoup.find_all('td', {'class':'snapshot-td2'})))            
            except HTTPError:
                print("{} - not found".format(url))