Skip to content

Commit 2ca96b7

Browse files
MRJPEREZRMaximSmolskiypre-commit-ci[bot]
authored
current_stock_price test added (TheAlgorithms#12390)
* adding test to web_programming/current_stock_price * adding test to web_programming/current_stock_price * Update current_stock_price.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update current_stock_price.py --------- Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 68b4c6b commit 2ca96b7

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

web_programming/current_stock_price.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
import requests
22
from bs4 import BeautifulSoup
33

4+
"""
5+
Get the HTML code of finance yahoo and select the current qsp-price
6+
Current AAPL stock price is 228.43
7+
Current AMZN stock price is 201.85
8+
Current IBM stock price is 210.30
9+
Current GOOG stock price is 177.86
10+
Current MSFT stock price is 414.82
11+
Current ORCL stock price is 188.87
12+
"""
13+
414

515
def stock_price(symbol: str = "AAPL") -> str:
16+
"""
17+
>>> stock_price("EEEE")
18+
'-'
19+
>>> isinstance(float(stock_price("GOOG")),float)
20+
True
21+
"""
622
url = f"https://finance.yahoo.com/quote/{symbol}?p={symbol}"
723
yahoo_finance_source = requests.get(
824
url, headers={"USER-AGENT": "Mozilla/5.0"}, timeout=10
925
).text
1026
soup = BeautifulSoup(yahoo_finance_source, "html.parser")
11-
specific_fin_streamer_tag = soup.find("fin-streamer", {"data-test": "qsp-price"})
27+
specific_fin_streamer_tag = soup.find("fin-streamer", {"data-testid": "qsp-price"})
1228

1329
if specific_fin_streamer_tag:
1430
text = specific_fin_streamer_tag.get_text()
@@ -18,5 +34,9 @@ def stock_price(symbol: str = "AAPL") -> str:
1834

1935
# Search for the symbol at https://finance.yahoo.com/lookup
2036
if __name__ == "__main__":
37+
from doctest import testmod
38+
39+
testmod()
40+
2141
for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split():
2242
print(f"Current {symbol:<4} stock price is {stock_price(symbol):>8}")

0 commit comments

Comments
 (0)