Skip to content

Commit 9945b8d

Browse files
authored
fix build current_stock_price.py
1 parent c3d4b9e commit 9945b8d

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

web_programming/current_stock_price.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
# ]
77
# ///
88

9-
import httpx
9+
from doctest import testmod
10+
11+
import requests
1012
from bs4 import BeautifulSoup
1113

14+
1215
"""
1316
Get the HTML code of finance yahoo and select the current qsp-price
1417
Current AAPL stock price is 228.43
@@ -28,20 +31,22 @@ def stock_price(symbol: str = "AAPL") -> str:
2831
True
2932
"""
3033
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+
try:
35+
yahoo_finance_source = requests.get(
36+
url, headers={"USER-AGENT": "Mozilla/5.0"}, timeout=10
37+
).text
38+
except requests.exceptions.RequestException:
39+
return "- "
40+
3441
soup = BeautifulSoup(yahoo_finance_source, "html.parser")
3542

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

4047

4148
# Search for the symbol at https://finance.yahoo.com/lookup
4249
if __name__ == "__main__":
43-
from doctest import testmod
44-
4550
testmod()
4651

4752
for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split():

0 commit comments

Comments
 (0)