2019-11-23 12:54:06 +00:00
|
|
|
import requests
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
|
|
|
|
|
def stock_price(symbol: str = "AAPL") -> str:
|
2023-08-12 21:58:37 +00:00
|
|
|
url = f"https://finance.yahoo.com/quote/{symbol}?p={symbol}"
|
2024-04-21 17:34:18 +00:00
|
|
|
yahoo_finance_source = requests.get(
|
|
|
|
url, headers={"USER-AGENT": "Mozilla/5.0"}, timeout=10
|
|
|
|
).text
|
2023-08-12 21:58:37 +00:00
|
|
|
soup = BeautifulSoup(yahoo_finance_source, "html.parser")
|
|
|
|
specific_fin_streamer_tag = soup.find("fin-streamer", {"data-test": "qsp-price"})
|
2019-11-23 12:54:06 +00:00
|
|
|
|
2023-08-12 21:58:37 +00:00
|
|
|
if specific_fin_streamer_tag:
|
|
|
|
text = specific_fin_streamer_tag.get_text()
|
|
|
|
return text
|
|
|
|
return "No <fin-streamer> tag with the specified data-test attribute found."
|
2019-11-23 12:54:06 +00:00
|
|
|
|
2023-08-12 21:58:37 +00:00
|
|
|
|
|
|
|
# Search for the symbol at https://finance.yahoo.com/lookup
|
2019-11-23 12:54:06 +00:00
|
|
|
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}")
|