Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions web_programming/current_stock_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
# ]
# ///

import httpx
from doctest import testmod

from bs4 import BeautifulSoup
import requests

"""
Get the HTML code of finance yahoo and select the current qsp-price
Expand All @@ -28,20 +30,22 @@ def stock_price(symbol: str = "AAPL") -> str:
True
"""
url = f"https://finance.yahoo.com/quote/{symbol}?p={symbol}"
yahoo_finance_source = httpx.get(
url, headers={"USER-AGENT": "Mozilla/5.0"}, timeout=10, follow_redirects=True
).text
try:
yahoo_finance_source = requests.get(
url, headers={"USER-AGENT": "Mozilla/5.0"}, timeout=10
).text
except requests.exceptions.RequestException:
return "- "

soup = BeautifulSoup(yahoo_finance_source, "html.parser")

if specific_fin_streamer_tag := soup.find("span", {"data-testid": "qsp-price"}):
return specific_fin_streamer_tag.get_text()
return "No <fin-streamer> tag with the specified data-testid attribute found."
return "- "


# Search for the symbol at https://finance.yahoo.com/lookup
if __name__ == "__main__":
from doctest import testmod

testmod()

for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split():
Expand Down
Loading