2

I have recently started learning python and one of my first project is to get live stock prices from Google finance using beautifulsoup. Basically I am looking up for a stock and setting a price alert.

here is what my code looks like.

import requests
import time
import tkinter
from bs4 import BeautifulSoup

def st_Price(symbol):
    baseurl = 'http://google.com/finance/quote/'
    URL = baseurl + symbol + ":NSE?hl=en&gl=in"
    
    page = requests.get(URL)
    
    soup = BeautifulSoup(page.content, 'html.parser')
    
    results = soup.find(class_="YMlKec fxKbKc")

    result = results.__str__()
    #print(result)

    res = result.split("₹")[1].split("<")[0]

    res_flt = float(res.replace(",",""))
      
    return res_flt
        
def main():
    
    sym = input("Enter Stock Symbol : ")
    price = input("Enter desired price : ")
    
    
    x = st_Price(sym)
    
    while x < float(price):
        print(x)
        t1 = time.perf_counter()
        x = st_Price(sym)
        t2 = time.perf_counter()
        print("Internal refresh time is {}".format(t2-t1))
    else:
        print("The Stock {} achieved price greater than {}".format(sym,x))
        root = tkinter.Tk()
        root.geometry("150x150")
        tkinter.messagebox.showinfo(title="Price Alert",message="Stock Price {} greater Than {}".format(x,price))
        root.destroy()
    

if __name__ == "__main__":
    main()

I am looking up following class in the Page HTML:

HTML element for the Stock

The code works perfectly fine but it takes too much time to fetch the information:

Enter Stock Symbol : INFY

Enter desired price : 1578
1574.0
Internal refresh time is 9.915285099999892
1574.0
Internal refresh time is 7.2284357999997155

I am not too much familiar with HTML. By referring online documentation I was able to figure out how to scrape necessary part.

Is there any way to reduce the time to fetch the data ?

GinSan
  • 21
  • 1
  • since the bottleneck seems to be the request i dont think you cant do much about it. Maybe you can split the tasks of fetching the html and parsing it, so you do the fetching first (and maybe periodic into local file or what every storage you prefer) and parse it from there and decouple the the parsing that way. That might not the best solution if you need it every other second – Markus Rosjat Jun 27 '21 at 11:59
  • @Markus ,I found something here: https://stackoverflow.com/a/62218607/16326666 So I changed one line of my code from `soup = BeautifulSoup(page.content, 'html.parser')` to `soup = BeautifulSoup(page.Text, 'html.parser')` and it the refresh went less than 2 seconds. Since I do not understand HTML parsing too much, it is hard for me to understand why it works. – GinSan Jun 29 '21 at 12:58
  • mostlikely something get skiped when you ask for .Text, maybe all the head stuff with the metadata. But if 2 sec is ok for your need then you good to go i guess ;) – Markus Rosjat Jun 29 '21 at 13:05
  • well it seems .test formats the content into "html" style format by translate all the tokens like \t or \n into spaces and newlines. This in turn seems to speed up the html parsing which makes sense – Markus Rosjat Jun 29 '21 at 13:16

0 Answers0