MCPcopy
hub / github.com/TheAlgorithms/Python / stock_price

Function stock_price

web_programming/current_stock_price.py:23–38  ·  view source on GitHub ↗

>>> stock_price("EEEE") 'No tag with the specified data-testid attribute found.' >>> isinstance(float(stock_price("GOOG")),float) True

(symbol: str = "AAPL")

Source from the content-addressed store, hash-verified

21
22
23def stock_price(symbol: str = "AAPL") -> str:
24 """
25 >>> stock_price("EEEE")
26 'No <fin-streamer> tag with the specified data-testid attribute found.'
27 >>> isinstance(float(stock_price("GOOG")),float)
28 True
29 """
30 url = f"https://finance.yahoo.com/quote/{symbol}?p={symbol}"
31 yahoo_finance_source = httpx.get(
32 url, headers={"USER-AGENT": "Mozilla/5.0"}, timeout=10, follow_redirects=True
33 ).text
34 soup = BeautifulSoup(yahoo_finance_source, "html.parser")
35
36 if specific_fin_streamer_tag := soup.find("span", {"data-testid": "qsp-price"}):
37 return specific_fin_streamer_tag.get_text()
38 return "No <fin-streamer> tag with the specified data-testid attribute found."
39
40
41# Search for the symbol at https://finance.yahoo.com/lookup

Callers 1

Calls 2

getMethod · 0.45
findMethod · 0.45

Tested by

no test coverage detected