Web Scraping – Stock Data Scraping Using Python 3

Stock Data : The stock market is the best performing asset class by far. To invest in stock market , we don’t need lump sum amount so anyone can buy/sell shares at fraction of cost (as commission). While looking for buying quality stocks, looking at the last stock value is very important along with other parameters.

Web Scraping : It means data extraction from websites. It is generally used to gather a specific data which can be analyzed and used for various purposes. In this article data will be scraped from the financial websites without browsing each web page separately.

If we need to analyze say 10 different shares while monitoring its price daily, one way is daily go to websites and check each ticker symbol’s price which is tedious. Second method is ¬†create an automated routine which gives this information just by running it or schedule it. This way it is possible to create the your own database also say a month’s performance for a particular stock etc.

Below are the steps involved in creating a automated script for web scraping:

  • Inspect the web page manually
  • Find the patterns in the data to be extracted
  • Fetching the web page (basically means downloading the data)
  • Extract the information using that pattern

[Note: This article is only for information purpose ]

Once the page is fetched, the extraction takes place. The content of the page is parsed, searched , reformatted and copied to the desired output source. In this process, information could be just a piece of value from the entire page. In our example, it is just the last stock price value.

import urllib.request
import re

symbollist = ["AAPL","ZEN","DE","MON","FWONA","AUO"]
while i <len(symbollist):
    #Masked the name of website, instead of abc use the website you want to scrape and adjust url as per the website's url
    url = "https://www.abc.com/stock/quote/" +symbollist[i] +"?q="+symbollist[i]
    regex = '<sup>\$</sup>(.+?)<span> USD</span>'
    pattern = re.compile(regex)
    htmlfile = urllib.request.urlopen(url)
    htmltext = htmlfile.read()
    stockprice = re.findall(pattern,str(htmltext))
    print("Price of ",symbollist[i],"=",stockprice)


Output :

Price of AAPL = [‘144.15’]
Price of ZEN = [‘27.86’]
Price of DE = [‘108.20’]
Price of MON = [‘113.95’]
Price of FWONA = [‘32.27’]
Price of AUO = [‘3.90’]

In the next page , we will be having mini project in Web Scraping using python…Next>>>>

Please share your thoughts on the articles. If you like it, please click like, share or comment. Happy reading…


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s