Skip to content

Commit 8681d06

Browse files
Suman2023pre-commit-ci[bot]
authored andcommitted
updated the URL and HTML tags for scrapping yahoo finance (TheAlgorithms#8942)
* updated the url and tags for yahoo finance * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updated to return the error text --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 6983582 commit 8681d06

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

web_programming/current_stock_price.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33

44

55
def stock_price(symbol: str = "AAPL") -> str:
6-
url = f"https://in.finance.yahoo.com/quote/{symbol}?s={symbol}"
7-
soup = BeautifulSoup(requests.get(url).text, "html.parser")
8-
class_ = "My(6px) Pos(r) smartphone_Mt(6px)"
9-
return soup.find("div", class_=class_).find("span").text
6+
url = f"https://finance.yahoo.com/quote/{symbol}?p={symbol}"
7+
yahoo_finance_source = requests.get(url, headers={"USER-AGENT": "Mozilla/5.0"}).text
8+
soup = BeautifulSoup(yahoo_finance_source, "html.parser")
9+
specific_fin_streamer_tag = soup.find("fin-streamer", {"data-test": "qsp-price"})
1010

11+
if specific_fin_streamer_tag:
12+
text = specific_fin_streamer_tag.get_text()
13+
return text
14+
return "No <fin-streamer> tag with the specified data-test attribute found."
1115

16+
17+
# Search for the symbol at https://finance.yahoo.com/lookup
1218
if __name__ == "__main__":
1319
for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split():
1420
print(f"Current {symbol:<4} stock price is {stock_price(symbol):>8}")

0 commit comments

Comments
 (0)