Christian Schou
  • Home
  • Blog
    • Programming
      • C#
      • PowerShell
      • Python
      • SQL
    • WordPress
      • Tutorials
    • Cloud
    • Home Automation
      • Home Assistant
        • Node-Red
    • Career
  • Services
  • Glossary
  • About
No Result
View All Result
Christian Schou
  • Home
  • Blog
    • Programming
      • C#
      • PowerShell
      • Python
      • SQL
    • WordPress
      • Tutorials
    • Cloud
    • Home Automation
      • Home Assistant
        • Node-Red
    • Career
  • Services
  • Glossary
  • About
No Result
View All Result
Christian Schou
No Result
View All Result
Home Programming Python
stock price scraper

How to make a STOCK price scraper command-line application using Python and Web Scraping

by Christian
6. Dezember 2021
in Python
0

If you like me like to check the stock prices from time to time, but don’t want to navigate around the websites to find the details you are looking for, then this CLI application might be the perfect tool for you. Thanks to Python it’s easy for us to get data from the internet using BeautifulSoup. Imagine you would like the latest price for Microsoft, then the only command you would need to enter is: python .\stockPrice.py MSFT inside your terminal. In this tutorial, I will teach you how to scrape stock prices from Yahoo Finance in only 24 lines.

Inhaltsverzeichnis
  1. Create the python file and install BS4 (BeautifulSoup)
    • Install BeautifulSoup through PowerShell with PIP
    • Create the python file stockPrice.py
  2. Python Web Scraping Stock Price
  3. Test the Python Stock Web Scraper
    • Check Microsoft Stock Price
    • Error testing
      • Too many tickers
      • No tickers
      • Invalid ticker
  4. Summary

Create the python file and install BS4 (BeautifulSoup)

Install BeautifulSoup through PowerShell with PIP

The first thing you have to do is install BS4 using PIP. The command for doing this is pip install bs4 – you should get a result like mine below when doing it:

PS C:\Users\chs\Desktop> pip install bs4            
Defaulting to user installation because normal site-packages is not writeable
Collecting bs4
  Downloading bs4-0.0.1.tar.gz (1.1 kB)
Collecting beautifulsoup4
  Downloading beautifulsoup4-4.10.0-py3-none-any.whl (97 kB)
     |████████████████████████████████| 97 kB 1.3 MB/s
Collecting soupsieve>1.2
  Downloading soupsieve-2.3-py3-none-any.whl (37 kB)
Using legacy 'setup.py install' for bs4, since package 'wheel' is not installed.
Installing collected packages: soupsieve, beautifulsoup4, bs4
    Running setup.py install for bs4 ... done
Successfully installed beautifulsoup4-4.10.0 bs4-0.0.1 soupsieve-2.3

Create the python file stockPrice.py

Now we have to make the fun stuff (coding). Go ahead and create a new python file and name it stockPrice or what you prefer and place it somewhere you can remember (location doesn’t matter). First, we need to make our imports including the newly installed bs4 library. The first four lines should contain this code:

import os
import sys
import requests
from bs4 import BeautifulSoup

In order to make the application more dynamic, our user has to enter a ticker (stock identifier). By making use of the sys library, we can check if the user actually entered a value for the ticker. If the user doesn’t enter a ticker, we would like to return an error message and also do it if the user forgets to enter a ticker for the stock.

When the ticker has been entered, we would like to store it in a variable so that we can access the value later when making a web request to Yahoo Finance.

if len(sys.argv) > 2:
    print('You are only allowed to enter one ticker at a time. Please try again.')
    sys.exit()

if len(sys.argv) < 2:
    print('You have to provide a ticker for the stock. Please try again.')
    sys.exit()

ticker = sys.argv[1]

With the ticker in place, we can now go ahead and make a request for our ticker at Yahoo Finance. The URL request has to be dynamic in order for it to change depending on the user input.

At line 14 we stored the ticker inside a variable, let’s use it. Create a new variable named stockUrl and add the following code to it: 'https://finance.yahoo.com/quote/' + ticker + '?p=' + ticker + '&.tsrc=fin-srch'. This will result in URLs that look like the following:

  • Apple – https://finance.yahoo.com/quote/AAPL?p=AAPL&.tsrc=fin-srch
  • Microsoft – https://finance.yahoo.com/quote/MSFT?p=MSFT&.tsrc=fin-srch
  • Tesla – https://finance.yahoo.com/quote/TSLA?p=TSLA&.tsrc=fin-srch

When making a request, we get a response – let’s store that inside a new variable named stockResponse. This variable is also the one responsible for making the request with our Stock ticker URL. It will look like the following:

stockURL = 'https://finance.yahoo.com/quote/' + ticker + '?p=' + ticker + '&.tsrc=fin-srch'
stockResponse = requests.get(stockURL)

So far so good – your stockPrice.py file at the moment, should look like the one below:

import os
import sys
import requests
from bs4 import BeautifulSoup

if len(sys.argv) > 2:
    print('You are only allowed to enter one ticker at a time. Please try again.')
    sys.exit()

if len(sys.argv) < 2:
    print('You have to provide a ticker for the stock. Please try again.')
    sys.exit()

ticker = sys.argv[1]

stockURL = 'https://finance.yahoo.com/quote/' + ticker + '?p=' + ticker + '&.tsrc=fin-srch'
stockResponse = requests.get(stockURL)

Python Web Scraping Stock Price

For this tutorial, I will be scraping the MSFT ticker. How do we know where the stock price is inside the returned response? For that, we need to make a visit at the stock page at Yahoo Finance. Mark the stock price at the webpage, right click it and select inspect.

So now we got a large popup in the right (or some cases bottom) of our screen. This is the webpage’s DOM. Here the browser has selected the stock price element on the website for us.

Now you have to select and copy the content inside the quotes of class for the element.

So far so good. Now we have the class property in our clipboard for the element at Yahoo Finance. this makes it possible for us to find the content of the element using soup.

Add a try-except “block” in your stockPrice.py file and add these three lines inside the try block.

soup = BeautifulSoup(stockResponse.text, 'html.parser')
price = soup.find('body').find(class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)')
print('The latest stock price for ' + ticker + ' is: ' + price.text.strip())

What happens above?

  • First line will parse the response data from our get request to Yahoo Finance.
  • Second line will find the stock price by using the element value we provided from the DOM of the webpage.
  • Thrid line will print the content of the price variable using price.text.strip method.

Inside the except block, add this print: print('Invalid ticker, please try again.'). You should now have a stockPrice.py file like this:

import os
import sys
import requests
from bs4 import BeautifulSoup

if len(sys.argv) > 2:
    print('You are only allowed to enter one ticker at a time. Please try again.')
    sys.exit()

if len(sys.argv) < 2:
    print('You have to provide a ticker for the stock. Please try again.')
    sys.exit()

ticker = sys.argv[1]

stockURL = 'https://finance.yahoo.com/quote/' + ticker + '?p=' + ticker + '&.tsrc=fin-srch'
stockResponse = requests.get(stockURL)

try:
    soup = BeautifulSoup(stockResponse.text, 'html.parser')
    price = soup.find('body').find(class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)')
    print('The latest stock price for ' + ticker + ' is: ' + price.text.strip())
except:
    print('Invalid ticker, please try again.')

Test the Python Stock Web Scraper

Below is the result of different types of tests made for the stock price scraper.

Check Microsoft Stock Price

PS C:\Users\chs\Desktop> python .\stockPrice.py MSFT
The latest stock price for MSFT is: 335.68

Error testing

Let’s see if our error handling code will inform the users if they make any mistakes.

Too many tickers

PS C:\Users\chs\Desktop> python .\stockPrice.py MSFT AAPL
You are only allowed to enter one ticker at a time. Please try again.

No tickers

PS C:\Users\chs\Desktop> python .\stockPrice.py          
You have to provide a ticker for the stock. Please try again.

Invalid ticker

PS C:\Users\chs\Desktop> python .\stockPrice.py CHRISTIAN
Invalid ticker, please try again.

Summary

You have now learned how to scrape data from a webpage by using elements, dynamic arguments, and displaying the data inside a command-line interface. How cool is that?

I hope this short tutorial has helped you gain some knowledge about how easy it is to use python beautiful soup to scrape the internet for data. If you got any questions or suggestions, please let me know in the comments. Happy coding! 🙂

Tags: BeautifulSoupCLIPIPPythonScrapingStocksWebWeb ScrapingYahoo
Previous Post

How To Build a Blazor Web App + In-depth theory about WASM and Razor

Next Post

My Top 17 Favorite GIT Commands for everyday use

Christian

Christian

Hello 👋 My name is Christian and I am 26 years old. I'm an educated Software Developer with a primary focus on C#, .NET Core, Python, and PowerShell. Currently, I'm expanding my skills in Software Robots and Cloud Architecture. In some of my spare time, I share my knowledge about tech stuff on my blog.

Related Posts

scrape prices, web scrape
Python

How to scrape prices from competitors e-commerce websites using Python

by Christian
7. Januar 2022
6

Running an e-commerce business in today's world can be quite a job. Everyone is trying to beat each other with...

Read more
delete files and folders

How to delete files and folders in a directory older than X days

9. Oktober 2021
Next Post
git commands

My Top 17 Favorite GIT Commands for everyday use

Schreibe einen Kommentar Antworten abbrechen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Christian Schou

Christian Schou

Software Developer

Hello - my name is Christian and I am 26 years old. I'm an educated Software Developer with a primary focus on C#, .NET Core, Python, and PowerShell. Currently, I'm expanding my skills in Software Robots and Cloud Architecture. In some of my spare time, I share my knowledge about tech stuff on my blog.

Recent articles

personal website
Career

Top 6 things to add on your personal website to get hired for a tech job

by Christian
7. August 2022
0

Back in the days before the internet was a thing like it is today, we used to have business cards...

Read more
watchdog

The #1 guide to show real-time .NET 6 logs for Web Apps and APIs in a modern way using WatchDog for Free

13. August 2022
get hired for a tech job

5 tips to help you get hired for a tech job

31. Juli 2022
restful web api

How to build a RESTful Web API using ASP.NET Core and Entity Framework Core (.NET 6)

25. Juli 2022
dynamically register entities

How to Dynamically Register Entities in DbContext by Extending ModelBuilder?

23. Juli 2022

Christian Schou

Software Developer

Hello - my name is Christian and I am 26 years old. I'm an educated Software Developer with a primary focus on C#, .NET Core, Python, and PowerShell. Currently, I'm expanding my skills in Software Robots and Cloud Architecture. In some of my spare time, I share my knowledge about tech stuff on my blog.

Recent articles

personal website

Top 6 things to add on your personal website to get hired for a tech job

7. August 2022
watchdog

The #1 guide to show real-time .NET 6 logs for Web Apps and APIs in a modern way using WatchDog for Free

13. August 2022
get hired for a tech job

5 tips to help you get hired for a tech job

31. Juli 2022
  • de_DEDeutsch
    • da_DKDansk
    • en_USEnglish
    • hi_INहिन्दी
    • pt_BRPortuguês do Brasil
  • Contact
  • Datenschutzrichtlinie
  • Nutzungsbedingungen

© 2022 Christian Schou - All rights reserved.

No Result
View All Result
  • Home
  • Blog
    • Programming
      • C#
      • PowerShell
      • Python
      • SQL
    • WordPress
      • Tutorials
    • Cloud
    • Home Automation
      • Home Assistant
    • Career
  • Services
  • Glossary
  • About

© 2022 Christian Schou - All rights reserved.

I use cookies on my website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
immer aktiv
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
CookieDauerBeschreibung
__gads1 year 24 daysThe __gads cookie, set by Google, is stored under DoubleClick domain and tracks the number of times users see an advert, measures the success of the campaign and calculates its revenue. This cookie can only be read from the domain they are set on and will not track any data while browsing through other sites.
_ga2 yearsThe _ga cookie, installed by Google Analytics, calculates visitor, session and campaign data and also keeps track of site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognize unique visitors.
_ga_0J2F6JVWSD2 yearsThis cookie is installed by Google Analytics.
_gat_gtag_UA_84232734_11 minuteSet by Google to distinguish users.
_gid1 dayInstalled by Google Analytics, _gid cookie stores information on how visitors use a website, while also creating an analytics report of the website's performance. Some of the data that are collected include the number of visitors, their source, and the pages they visit anonymously.
YouTube2 yearsYouTube sets this cookie via embedded youtube-videos and registers anonymous statistical data. I embed YouTube videos in my articles/tutorials - you won't get the full experience of the articles if this is deactivated.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
CookieDauerBeschreibung
IDE1 year 24 daysGoogle DoubleClick IDE cookies are used to store information about how the user uses the website to present them with relevant ads and according to the user profile.
test_cookie15 minutesThe test_cookie is set by doubleclick.net and is used to determine if the user's browser supports cookies.
VISITOR_INFO1_LIVE5 months 27 daysA cookie set by YouTube to measure bandwidth that determines whether the user gets the new or old player interface.
YSCsessionYSC cookie is set by Youtube and is used to track the views of embedded videos on Youtube pages.
yt-remote-connected-devicesneverYouTube sets this cookie to store the video preferences of the user using embedded YouTube video.
yt-remote-device-idneverYouTube sets this cookie to store the video preferences of the user using embedded YouTube video.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SPEICHERN & AKZEPTIEREN
Unterstützt von CookieYes Logo