Yahoo Finance is a good source for extracting financial data, be it – stock market data, trading prices or business-related news.
In this tutorial, we will extract the trading summary for a public company from Yahoo Finance ( like http://finance.yahoo.com/quote/AAPL?p=AAPL ). We’ll be extracting the following fields for this tutorial.
- Previous Close
- Day’s Range
- 52 Week Range
- Average Volume
- Market Cap
- PE Ratio
- Earning’s Date
- Dividend & Yield
- Ex-Dividend Date
- 1yr Target EST
Below is a screenshot of what data we’ll be extracting from Yahoo Finance.
- Construct the URL of the search results page from Yahoo Finance. For example, here is the one for Apple-http://finance.yahoo.com/quote/AAPL?p=AAPL
- Download HTML of the search result page using Python Requests
- Parse the page using LXML – LXML lets you navigate the HTML Tree Structure using Xpaths. We have predefined the XPaths for the details we need in the code.
- Save the data to a JSON file.
Install Python 3 and Pip
Here is a guide to install Python 3 in Linux – http://docs.python-guide.org/en/latest/starting/install3/linux/
Mac Users can follow this guide – http://docs.python-guide.org/en/latest/starting/install3/osx/
Windows Users go here – https://www.scrapehero.com/how-to-install-python3-in-windows-10/
For this web scraping tutorial using Python 3, we will need some packages for downloading and parsing the HTML. Below are the package requirements:
- PIP to install the following packages in Python (https://pip.pypa.io/en/stable/installing/ )
- Python Requests, to make requests and download the HTML content of the pages ( http://docs.python-requests.org/en/master/user/install/).
- Python LXML, for parsing the HTML Tree Structure using Xpaths ( Learn how to install that here – http://lxml.de/installation.html )
You can download the code from the link https://gist.github.com/scrapehero/516fc801a210433602fe9fd41a69b496 if the embed above does not work.
If you would like the code in Python 2 check out this link https://gist.github.com/scrapehero/b0c7426f85aeaba441d603bb81e1d0e2
Running the Scraper
Assume the script is named yahoo_finance.py. If you type in the script name in command prompt or terminal with a -h
python yahoo_finance.py -h
usage: yahoo_finance.py [-h] ticker
-h, --help show this help message and exit
The ticker argument is the ticker symbol or stock symbol to identify a company.
To find the stock data for Apple Inc we would put the argument like this:
python3 yahoo_finance.py aapl
This should create a JSON file called aapl-summary.json that will be in the same folder as the script.
The output file would look similar to this:
"Previous Close": "139.52",
"Bid": "138.69 x 100",
"Ask": "139.01 x 4600",
"Day's Range": "138.82 - 139.80",
"52 Week Range": "89.47 - 140.28",
"Avg. Volume": "28,451,631",
"Market Cap": "729.58B",
"PE Ratio (TTM)": "16.69",
"EPS (TTM)": 8.33,
"Earnings Date": "2017-04-24 to 2017-04-28",
"Dividend & Yield": "2.28 (1.63%)",
"Ex-Dividend Date": "N/A",
"1y Target Est": 142.48,
You can download the code at https://gist.github.com/scrapehero/516fc801a210433602fe9fd41a69b496
Let us know in the comments how this scraper worked for you.
This code should work for grabbing stock market data of most companies. However, if you want to scrape for thousands of pages and do it frequently (say, multiple times per hour) there are some important things you should be aware of, and you can read about them at How to build and run scrapers on a large scale and How to prevent getting blacklisted while scraping.
If you need some professional help with scraping complex websites contact us by filling up the form below.
Tell us about your complex web scraping projects
Turn websites into meaningful and structured data through our web data extraction service
Disclaimer: Any code provided in our tutorials is for illustration and learning purposes only. We are not responsible for how it is used and assume no liability for any detrimental usage of the source code. The mere presence of this code on our site does not imply that we encourage scraping or scrape the websites referenced in the code and accompanying tutorial. The tutorials only help illustrate the technique of programming web scrapers for popular internet websites. We are not obligated to provide any support for the code, however, if you add your questions in the comments section, we may periodically address them.