updated the url and tags for yahoo finance

This commit is contained in:
Suman 2023-08-10 20:22:35 +05:30
parent ae0fc85401
commit 7d27480e25

View File

@ -3,12 +3,19 @@ from bs4 import BeautifulSoup
def stock_price(symbol: str = "AAPL") -> str: def stock_price(symbol: str = "AAPL") -> str:
url = f"https://in.finance.yahoo.com/quote/{symbol}?s={symbol}" url = f"https://finance.yahoo.com/quote/{symbol}?p={symbol}"
soup = BeautifulSoup(requests.get(url).text, "html.parser") yahoo_finance_source = requests.get(url,headers={'USER-AGENT': "Mozilla/5.0"}).text
class_ = "My(6px) Pos(r) smartphone_Mt(6px)" soup = BeautifulSoup(yahoo_finance_source, "html.parser")
return soup.find("div", class_=class_).find("span").text 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 <fin-streamer> tag with the specified data-test attribute found.")
return "Not Found"
# Search for the symbol at https://finance.yahoo.com/lookup
if __name__ == "__main__": if __name__ == "__main__":
for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split():
print(f"Current {symbol:<4} stock price is {stock_price(symbol):>8}") print(f"Current {symbol:<4} stock price is {stock_price(symbol):>8}")