From 9945b8d00693e7a57eb942a9768b5a36ccaceb4c Mon Sep 17 00:00:00 2001 From: lighting9999 Date: Sat, 5 Jul 2025 17:41:50 +0800 Subject: [PATCH 1/2] fix build current_stock_price.py --- web_programming/current_stock_price.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/web_programming/current_stock_price.py b/web_programming/current_stock_price.py index 16b0b6772a9c..e5bc185639fa 100644 --- a/web_programming/current_stock_price.py +++ b/web_programming/current_stock_price.py @@ -6,9 +6,12 @@ # ] # /// -import httpx +from doctest import testmod + +import requests from bs4 import BeautifulSoup + """ Get the HTML code of finance yahoo and select the current qsp-price Current AAPL stock price is 228.43 @@ -28,20 +31,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 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(): From 10c24a15df175e323d683168c96288369bbe6846 Mon Sep 17 00:00:00 2001 From: lighting9999 Date: Sat, 5 Jul 2025 17:46:52 +0800 Subject: [PATCH 2/2] fix current_stock_price.py --- web_programming/current_stock_price.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/web_programming/current_stock_price.py b/web_programming/current_stock_price.py index e5bc185639fa..66ad7a2da8aa 100644 --- a/web_programming/current_stock_price.py +++ b/web_programming/current_stock_price.py @@ -8,9 +8,8 @@ from doctest import testmod -import requests from bs4 import BeautifulSoup - +import requests """ Get the HTML code of finance yahoo and select the current qsp-price