Skip to content

Commit 8eab6b3

Browse files
committed
adding test to web_programming/current_stock_price
1 parent 3e9ca92 commit 8eab6b3

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
import requests
1+
from doctest import testmod
2+
23
from bs4 import BeautifulSoup
4+
from requests import get
35

46

57
def stock_price(symbol: str = "AAPL") -> str:
8+
"""
9+
>>> stock_price("EEEE")
10+
'-'
11+
>>> isinstance(float(stock_price("GOOG")),float)
12+
True
13+
"""
614
url = f"https://finance.yahoo.com/quote/{symbol}?p={symbol}"
7-
yahoo_finance_source = requests.get(
15+
yahoo_finance_source = get(
816
url, headers={"USER-AGENT": "Mozilla/5.0"}, timeout=10
917
).text
1018
soup = BeautifulSoup(yahoo_finance_source, "html.parser")
11-
specific_fin_streamer_tag = soup.find("fin-streamer", {"data-test": "qsp-price"})
19+
specific_fin_streamer_tag = soup.find("fin-streamer", {"data-testid": "qsp-price"})
1220

1321
if specific_fin_streamer_tag:
1422
text = specific_fin_streamer_tag.get_text()
@@ -18,5 +26,6 @@ def stock_price(symbol: str = "AAPL") -> str:
1826

1927
# Search for the symbol at https://finance.yahoo.com/lookup
2028
if __name__ == "__main__":
29+
testmod()
2130
for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split():
2231
print(f"Current {symbol:<4} stock price is {stock_price(symbol):>8}")

0 commit comments

Comments
 (0)