From 7d27480e2571b279bae17816be394ddf4f9e4745 Mon Sep 17 00:00:00 2001 From: Suman Date: Thu, 10 Aug 2023 20:22:35 +0530 Subject: [PATCH 1/3] updated the url and tags for yahoo finance --- web_programming/current_stock_price.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/web_programming/current_stock_price.py b/web_programming/current_stock_price.py index df44da4ef351..dfce457e1b1c 100644 --- a/web_programming/current_stock_price.py +++ b/web_programming/current_stock_price.py @@ -3,12 +3,19 @@ def stock_price(symbol: str = "AAPL") -> str: - url = f"https://in.finance.yahoo.com/quote/{symbol}?s={symbol}" - soup = BeautifulSoup(requests.get(url).text, "html.parser") - class_ = "My(6px) Pos(r) smartphone_Mt(6px)" - return soup.find("div", class_=class_).find("span").text + url = f"https://finance.yahoo.com/quote/{symbol}?p={symbol}" + yahoo_finance_source = requests.get(url,headers={'USER-AGENT': "Mozilla/5.0"}).text + soup = BeautifulSoup(yahoo_finance_source, "html.parser") + specific_fin_streamer_tag = soup.find('fin-streamer', {'data-test': 'qsp-price'}) + if specific_fin_streamer_tag: + text = specific_fin_streamer_tag.get_text() + return text + else: + print("No tag with the specified data-test attribute found.") + return "Not Found" +# Search for the symbol at https://finance.yahoo.com/lookup if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(f"Current {symbol:<4} stock price is {stock_price(symbol):>8}") From df0e844223a1d3139d9b196ce20095450482ee32 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 10 Aug 2023 15:19:45 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/current_stock_price.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/web_programming/current_stock_price.py b/web_programming/current_stock_price.py index dfce457e1b1c..7fd40efeab85 100644 --- a/web_programming/current_stock_price.py +++ b/web_programming/current_stock_price.py @@ -4,9 +4,9 @@ def stock_price(symbol: str = "AAPL") -> str: url = f"https://finance.yahoo.com/quote/{symbol}?p={symbol}" - yahoo_finance_source = requests.get(url,headers={'USER-AGENT': "Mozilla/5.0"}).text + yahoo_finance_source = requests.get(url, headers={"USER-AGENT": "Mozilla/5.0"}).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-test": "qsp-price"}) if specific_fin_streamer_tag: text = specific_fin_streamer_tag.get_text() @@ -15,6 +15,7 @@ def stock_price(symbol: str = "AAPL") -> str: print("No tag with the specified data-test attribute found.") return "Not Found" + # Search for the symbol at https://finance.yahoo.com/lookup if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): From f4ae6fde40a345d1cb0d7f1904d12d20fc8ed9e4 Mon Sep 17 00:00:00 2001 From: Suman Date: Fri, 11 Aug 2023 16:10:51 +0530 Subject: [PATCH 3/3] updated to return the error text --- web_programming/current_stock_price.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/web_programming/current_stock_price.py b/web_programming/current_stock_price.py index dfce457e1b1c..51a0cce9f94a 100644 --- a/web_programming/current_stock_price.py +++ b/web_programming/current_stock_price.py @@ -11,9 +11,7 @@ def stock_price(symbol: str = "AAPL") -> str: if specific_fin_streamer_tag: text = specific_fin_streamer_tag.get_text() return text - else: - print("No tag with the specified data-test attribute found.") - return "Not Found" + return "No tag with the specified data-test attribute found." # Search for the symbol at https://finance.yahoo.com/lookup if __name__ == "__main__":