Member-only story

Web Scraping Yahoo Finance

Pull financial statements and stock data from any publicly traded company

Randy Macaraeg
Towards Data Science
5 min readFeb 1, 2020

--

Photo by Markus Spiske on Unsplash

The code to this blog can be found on my GitHub.

In the business world, it’s important to know the financial health of a company. Looking at the financial statements is a great way to get some insight into how well a company is doing.

In this blog, I’ll go over pulling the financial statements from Yahoo Finance for any company in their database in Python. Because Yahoo Finance uses JavaScript, we utilize a combination of BeautifulSoup and Selenium

Import the Libraries

Let’s start with the necessary libraries:

import pandas as pd
from bs4 import BeautifulSoup
import re
from selenium import webdriver
import chromedriver_binary
import string
pd.options.display.float_format = '{:.0f}'.format

Set up and run the driver

is_link = 'https://finance.yahoo.com/quote/AAPL/financials?p=AAPL'driver = webdriver.Chrome()
driver.get(is_link)
html = driver.execute_script('return document.body.innerHTML;')
soup = BeautifulSoup(html,'lxml')

--

--

Towards Data Science
Towards Data Science

Published in Towards Data Science

Your home for data science and AI. The world’s leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals.

Responses (3)