Html Parsing and Beautiful Soup

I have recently been learning Html Parsing and combining it with beautiful soup. The other day I made a stock price scraper that tells you if the price is higher or lower than the last update. Here is the code:

from bs4 import BeautifulSoup
import requests
from time import sleep

ticker = input()
ticker = ticker.upper()

previous_value = None

while True:
    response = requests.get(f"https://www.google.com/finance/quote/{ticker}:NASDAQ?hl=en")
    soup = BeautifulSoup(response.text, 'html.parser')

    value = soup.find_all("div", class_="YMlKec fxKbKc")
    current_value = float(value[0].text.replace("$", ""))
    
    print(current_value)
    if previous_value is not None:
        if current_value > previous_value:
            print("Sell")
        elif current_value < previous_value:
            print("Buy")
        else:
            print("Hold")
    
    previous_value = current_value

    sleep(10)

And what the code does when run:

tsla
253.91
254.4
Sell


Posted

in

by

Tags:

Comments

Leave a Reply

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