< Back to Blog Overview

Web Scraping Yahoo Finance By Python: A Step By Step Guide

09-05-2020

Stock market data scraping is done widely around the globe today. With Python being a versatile language, it can be used for a wide variety of tasks, including web scraping.

In this blog post, we’ll scrape Yahoo finance from the web using the Python programming language.

We’ll go over some of the basics of web scraping, and then we’ll get into how to scrape financial data specifically. We’ll also talk about some of the challenges in it, and how to overcome them.

web scraping yahoo finance with python
Scrape Yahoo Finance

We will code a scraper for that. Using that scraper you would be able to extract data from financial statements. As you know I like to make things pretty simple, for that, I will also be using a web scraper which will increase your scraping efficiency.

Why this tool? This tool will help us to scrape dynamic websites using millions of rotating proxies so that we don’t get blocked. It also provides a captcha clearing facility. It uses headerless chrome to scrape dynamic websites.

Know More: Why Price Scraping is done!!

Why Scrape Financial Statements from Yahoo Finance?

There are a number of reasons why you might want to scrape financial statements from the web. Perhaps you’re trying to track the performance of a publicly-traded company, or you’re looking for information on a potential investment.

In any case, gathering data on financial statements can be a tedious and time-consuming process.

That’s where web scraping comes in. By writing a simple Python script, you can automate the task of extracting data from financial statements. Not only will this save you time, but it will also allow you to gather data more frequently, giving you a more up-to-date picture of a company’s financial health.

Requirements

Generally, web scraping is divided into two parts:

  1. Fetching data by making an HTTP request
  2. Extracting important data by parsing the HTML DOM

Libraries & Tools

  1. Beautiful Soup is a Python library for pulling data out of HTML and XML files. BeautifulSoup is a library for parsing HTML and XML documents. It provides a number of methods for accessing and manipulating the data in the document. We’ll be using the BeautifulSoup library to parse the HTML document that we get back from the website.
  2. Requests allow you to send HTTP requests very easily. Requests is a library that allows you to send HTTP requests. We’ll be using the requests library to make a GET request to the website that we want to scrape.
  3. Web scraping tool to extract the HTML code of the target URL.

Putting it all together

Now that we’ve seen how to use the BeautifulSoup and requests libraries, let’s see how to put it all together to scrape financial statements from the web.

We’ll start by making a GET request to the website that we want to scrape. We’ll then use the BeautifulSoup library to parse the HTML document that we get back. Finally, we’ll extract the data that we want from the parsed document.

Setup

Our setup is pretty simple. Just create a folder and install Beautiful Soup & requests. For creating a folder and installing libraries type below given commands. I am assuming that you have already installed Python 3.x.

mkdir scraper
pip install beautifulsoup4
pip install requests

Now, create a file inside that folder by any name you like. I am using scraping.py.Firstly, you have to sign up for the scrapingdog API. It will provide you with 1000 FREE credits. Then just import Beautiful Soup & requests in your file. like this.

from bs4 import BeautifulSoup
import requests

What we are going to scrape

Here is the list of fields we will be extracting:

  1. Previous Close
  2. Open
  3. Bid
  4. Ask
  5. Day’s Range
  6. 52 Week Range
  7. Volume
  8. Avg. Volume
  9. Market Cap
  10. Beta
  11. PE Ratio
  12. EPS
  13. Earnings Rate
  14. Forward Dividend & Yield
  15. Ex-Dividend & Date
  16. 1y target EST
1 xbt1TNYy74mUVQjia tKJw
Yahoo Finance

Preparing the Food

Now, since we have all the ingredients to prepare the scraper, we should make a GET request to the target URL to get the raw HTML data. If you are not familiar with the scraping tool, I would urge you to go through its documentation. Now we will scrape Yahoo Finance for financial data using requests library as shown below.

r = requests.get("<a href="https://api.scrapingdog.com/scrape?api_key=5ea541dcacf6581b0b4b4042&amp;url=https://finance.yahoo.com/quote/AMZN?p=AMZN&amp;.tsrc=fin-srch" rel="noreferrer noopener" target="_blank">https://api.scrapingdog.com/scrape?api_key=&lt;your-api-key&gt;&amp;url=https://finance.yahoo.com/quote/AMZN?p=AMZN&amp;.tsrc=fin-srch</a>").text

This will provide you with an HTML code of that target URL. Now, you have to use BeautifulSoup to parse HTML.

soup = BeautifulSoup(r,’html.parser’)

Now, on the entire page, we have four “tbody” tags. We are interested in the first two because we currently don’t need the data available inside the third & fourth “tbody” tags.

python web scraping financial data
tbody tag on website

First, we will find out all those “tbody” tags using variable “soup”.

alldata = soup.find_all(“tbody”)
tr & td tags inside tbody

As you can notice that the first two “tbody” has 8 “tr” tags and every “tr” tag has two “td” tags.

try:
 table1 = alldata[0].find_all(“tr”)
except:
 table1=None

try:
 table2 = alldata[1].find_all(“tr”)
except:
 table2 = None

Now, each “tr” tag has two “td” tags. The first td tag consists of the name of the property and the other one has the value of that property. It’s something like a key-value pair.

1 6nYNPNzJsUb wTNtEBtX g
data inside td tags

At this point, we are going to declare a list and a dictionary before starting a for loop.

l={}
u=list()

For making the code simple I will be running two different “for” loops for each table. First for “table1”

for i in range(0,len(table1)):
 try:
   table1_td = table1[i].find_all(“td”)
 except:
   table1_td = None 

l[table1_td[0].text] = table1_td[1].text 

u.append(l)
 l={}

Now, what we have done is we are storing all the td tags in a variable “table1_td”. And then we are storing the value of the first & second td tag in a “dictionary”. Then we are pushing the dictionary into a list. Since we don’t want to store duplicate data we are going to make dictionary empty at the end. Similar steps will be followed for “table2”.

for i in range(0,len(table2)):
 try:
   table2_td = table2[i].find_all(“td”)
 except:
   table2_td = None

 l[table2_td[0].text] = table2_td[1].text

u.append(l)
 l={}

Then at the end when you print the list “u” you get a JSON response.

{
 “Yahoo finance”: [
 {
   “Previous Close”: “2,317.80”
 },
 {
   “Open”: “2,340.00”
 },
 {
   “Bid”: “0.00 x 1800”
 },
 {
   “Ask”: “2,369.96 x 1100”
 },
 {
   “Day’s Range”: “2,320.00–2,357.38”
 },
 {
   “52 Week Range”: “1,626.03–2,475.00”
 },
 {
   “Volume”: “3,018,351”
 },
 {
   “Avg. Volume”: “6,180,864”
 },
 {
   “Market Cap”: “1.173T”
 },
 {
   “Beta (5Y Monthly)”: “1.35”
 },
 {
   “PE Ratio (TTM)”: “112.31”
 },
 {
   “EPS (TTM)”: “20.94”
 },
 {
   “Earnings Date”: “Jul 23, 2020 — Jul 27, 2020”
 },
 {
   “Forward Dividend &amp; Yield”: “N/A (N/A)”
 },
 {
   “Ex-Dividend Date”: “N/A”
 },
 {
   “1y Target Est”: “2,645.67”
 }
 ]
}

Isn’t that amazing? We managed to scrape Yahoo finance in just 5 minutes of setup. We have an array of python Objects containing the financial data of the company Amazon. In this way, we can scrape the data from any website.

The Benefits of Scraping Financial Statements with Python

  1. Python is a versatile scripting language that is widely used in many different programming contexts.
  1. Python’s “requests” and “BeautifulSoup” libraries make it easy to download and process web pages for data scraping purposes.
  1. Python can be used to scrap financial statements from websites in a quick and efficient manner.
  1. The data from financial statements can be used for various financial analyses and research purposes.
  1. Python’s “pandas” library provides excellent tools for data analysis and manipulation, making it ideal for use with financial data.

Thus, Python is an excellent tool for web scraping financial statements from websites. It is quick, efficient, and versatile, making it a great choice for those looking to gather data for financial analysis and research.

Conclusion

In this article, we understood how we can scrape data using data scraping tool & BeautifulSoup regardless of the type of website. Feel free to comment and ask me anything. You can follow me on Twitter and Medium. Thanks for reading and please hit the like button!

Frequently Asked Questions

Yes, Yahoo Finance API is set of libraries to obtain historical and real time data for financial market and products.

Additional Resources

And there’s the list! At this point, you should feel comfortable writing your first web scraper to gather data from any website. Here are a few additional resources that you may find helpful during your web scraping journey:

Manthan Koolwal

My name is Manthan Koolwal and I am the CEO of scrapingdog.com. I love creating scraper and seamless data pipelines.
Scrapingdog Logo

Try Scrapingdog for Free!

Free 1000 API calls of testing.

No credit card required!