From 8eab6b355e6f51e0039b962fa73ca36e627d7db7 Mon Sep 17 00:00:00 2001 From: Julian Perez Ramirez Date: Wed, 20 Nov 2024 15:46:21 +0100 Subject: [PATCH 1/5] adding test to web_programming/current_stock_price --- web_programming/current_stock_price.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/web_programming/current_stock_price.py b/web_programming/current_stock_price.py index 9567c05b0558..ed760dfa338f 100644 --- a/web_programming/current_stock_price.py +++ b/web_programming/current_stock_price.py @@ -1,14 +1,22 @@ -import requests +from doctest import testmod + from bs4 import BeautifulSoup +from requests import get def stock_price(symbol: str = "AAPL") -> str: + """ + >>> stock_price("EEEE") + '-' + >>> isinstance(float(stock_price("GOOG")),float) + True + """ url = f"https://finance.yahoo.com/quote/{symbol}?p={symbol}" - yahoo_finance_source = requests.get( + yahoo_finance_source = get( url, headers={"USER-AGENT": "Mozilla/5.0"}, timeout=10 ).text soup = BeautifulSoup(yahoo_finance_source, "html.parser") - specific_fin_streamer_tag = soup.find("fin-streamer", {"data-test": "qsp-price"}) + specific_fin_streamer_tag = soup.find("fin-streamer", {"data-testid": "qsp-price"}) if specific_fin_streamer_tag: text = specific_fin_streamer_tag.get_text() @@ -18,5 +26,6 @@ def stock_price(symbol: str = "AAPL") -> str: # Search for the symbol at https://finance.yahoo.com/lookup if __name__ == "__main__": + testmod() for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(f"Current {symbol:<4} stock price is {stock_price(symbol):>8}") From 950ea9cb45b0735c324b147be6fb29272e220114 Mon Sep 17 00:00:00 2001 From: Julian Perez Ramirez Date: Wed, 20 Nov 2024 15:48:57 +0100 Subject: [PATCH 2/5] adding test to web_programming/current_stock_price --- web_programming/current_stock_price.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/web_programming/current_stock_price.py b/web_programming/current_stock_price.py index ed760dfa338f..3de7420f6925 100644 --- a/web_programming/current_stock_price.py +++ b/web_programming/current_stock_price.py @@ -3,6 +3,16 @@ from bs4 import BeautifulSoup from requests import get +""" +Get the HTML code of finance yahoo and select the current qsp-price +Current AAPL stock price is 228.43 +Current AMZN stock price is 201.85 +Current IBM stock price is 210.30 +Current GOOG stock price is 177.86 +Current MSFT stock price is 414.82 +Current ORCL stock price is 188.87 +""" + def stock_price(symbol: str = "AAPL") -> str: """ From 04f998d2ff0f9e894ccf9ae6327ee560472fef4d Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 30 Dec 2024 13:32:15 +0300 Subject: [PATCH 3/5] Update current_stock_price.py --- web_programming/current_stock_price.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/web_programming/current_stock_price.py b/web_programming/current_stock_price.py index 3de7420f6925..3bfae3b28984 100644 --- a/web_programming/current_stock_price.py +++ b/web_programming/current_stock_price.py @@ -1,7 +1,5 @@ -from doctest import testmod - +import requests from bs4 import BeautifulSoup -from requests import get """ Get the HTML code of finance yahoo and select the current qsp-price @@ -22,11 +20,11 @@ def stock_price(symbol: str = "AAPL") -> str: True """ url = f"https://finance.yahoo.com/quote/{symbol}?p={symbol}" - yahoo_finance_source = get( + yahoo_finance_source = requests.get( url, headers={"USER-AGENT": "Mozilla/5.0"}, timeout=10 ).text soup = BeautifulSoup(yahoo_finance_source, "html.parser") - specific_fin_streamer_tag = soup.find("fin-streamer", {"data-testid": "qsp-price"}) + specific_fin_streamer_tag = soup.find("fin-streamer", {"data-test": "qsp-price"}) if specific_fin_streamer_tag: text = specific_fin_streamer_tag.get_text() @@ -36,6 +34,8 @@ def stock_price(symbol: str = "AAPL") -> str: # 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(): print(f"Current {symbol:<4} stock price is {stock_price(symbol):>8}") From 879e553ef0e364d7870aa231b2379b8d3bb2318d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 30 Dec 2024 10:32:37 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/current_stock_price.py | 1 + 1 file changed, 1 insertion(+) diff --git a/web_programming/current_stock_price.py b/web_programming/current_stock_price.py index 3bfae3b28984..3afea87f414f 100644 --- a/web_programming/current_stock_price.py +++ b/web_programming/current_stock_price.py @@ -35,6 +35,7 @@ def stock_price(symbol: str = "AAPL") -> str: # 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 4965bef50eeacd7202e230b19e1d934515e8f8ca Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 30 Dec 2024 13:35:13 +0300 Subject: [PATCH 5/5] Update current_stock_price.py --- web_programming/current_stock_price.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/current_stock_price.py b/web_programming/current_stock_price.py index 3afea87f414f..d0a65e9aac84 100644 --- a/web_programming/current_stock_price.py +++ b/web_programming/current_stock_price.py @@ -24,7 +24,7 @@ def stock_price(symbol: str = "AAPL") -> str: url, headers={"USER-AGENT": "Mozilla/5.0"}, timeout=10 ).text soup = BeautifulSoup(yahoo_finance_source, "html.parser") - specific_fin_streamer_tag = soup.find("fin-streamer", {"data-test": "qsp-price"}) + specific_fin_streamer_tag = soup.find("fin-streamer", {"data-testid": "qsp-price"}) if specific_fin_streamer_tag: text = specific_fin_streamer_tag.get_text()