Skip to content

Commit aa63803

Browse files
authored
Update current_stock_price.py
1 parent d857ae2 commit aa63803

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

web_programming/current_stock_price.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
# /// script
2-
# requires-python = ">=3.13"
3-
# dependencies = [
4-
# "beautifulsoup4",
5-
# "httpx",
6-
# ]
7-
# ///
8-
9-
import httpx
1+
import requests
2+
103
from bs4 import BeautifulSoup
4+
from doctest import testmod
115

126
"""
137
Get the HTML code of finance yahoo and select the current qsp-price
@@ -28,20 +22,22 @@ def stock_price(symbol: str = "AAPL") -> str:
2822
True
2923
"""
3024
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
25+
try:
26+
yahoo_finance_source = requests.get(
27+
url, headers={"USER-AGENT": "Mozilla/5.0"}, timeout=10
28+
).text
29+
except requests.exceptions.RequestException:
30+
return '- '
31+
3432
soup = BeautifulSoup(yahoo_finance_source, "html.parser")
3533

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

4038

4139
# Search for the symbol at https://finance.yahoo.com/lookup
4240
if __name__ == "__main__":
43-
from doctest import testmod
44-
4541
testmod()
4642

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

0 commit comments

Comments
 (0)