Extract current stock price using web scraping in Python

In this tutorial, we will learn about extracting current stock prices from https://finance.yahoo.com/  using Python.

Installing the libraries

Initially, there are some libraries we need to install. Navigate to your command prompt and type the following lines separately.

pip install requests
pip install beautifulsoup

After the following libraries have been installed on your pc, you can import them into the code.


Required modules

Here we use the two most powerful libraries: requests and bs4.

  • The requests module is a Python library that allows you to send HTTP requests.
  • Beautiful Soup is a Python package for pulling data out of HTML and XML files.
import requests
from bs4 import BeautifulSoup

Scraping the website

creating a list of URLs from the yahoo finance website

Now, let us make a list of URLs in the variable ‘urls’.

Here, I have made a list of Google, Amazon, Netflix, Primoris Services Corporation, and Apple stocks.

urls = ['https://finance.yahoo.com/quote/GOOG/' , 
'https://finance.yahoo.com/quote/AMZN?p=AMZN&.tsrc=fin-srch' , 
'https://finance.yahoo.com/quote/NFLX?p=NFLX&.tsrc=fin-srch' , 
'https://finance.yahoo.com/quote/PRIM?p=PRIM&.tsrc=fin-srch' ,

later, we have to loop around the list.  With the help of the request module, we can access the response data and by using bs4, we can extract data from LXML.

Once if you had visited the website, we see that the title of the stock will be in the <h1> element.

We need to scrape the h1 element for the title of the stock. When we inspect for the stock price, we find div class and span class. So after scraping the data, we store it into the current-price variable.

for url in urls:
    headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64)
    AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'}
    html_page = requests.get(url,headers = headers)

    soup = BeautifulSoup(html_page.content,'lxml')
    header_info = soup.find_all("div",id='quote-header-info')[0]
    # extracting the h1 element as title name 
    title = header_info.find("h1").get_text()
    # extracting the current price according to the class and corresponding elements
    current_price = header_info.find('div' , class_='My(6px) Pos(r) 
    smartphone_Mt(6px)').find('span' ,class_ ='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) 

    print('Current price of '+title+'is : '+current_price);

To sum up, the intention of this program is to know the current price of your favorite stocks. you can also add more URLs to the list.


This is how the output looks after running the program.

Current price of Alphabet Inc. (GOOG)is : 2,838.42
Current price of Amazon.com, Inc. (AMZN)is : 3,469.15
Current price of Netflix, Inc. (NFLX)is : 598.72
Current price of Primoris Services Corporation (PRIM)is : 25.93
Current price of Apple Inc. (AAPL)is : 148.97

Leave a Reply

Your email address will not be published. Required fields are marked *