Agathe1489 commited on
Commit
72b7c98
·
verified ·
1 Parent(s): 85558ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -3,6 +3,7 @@ import datetime
3
  import requests
4
  import pytz
5
  import yaml
 
6
  from bs4 import BeautifulSoup
7
  from tools.final_answer import FinalAnswerTool
8
 
@@ -19,15 +20,13 @@ def get_stock_price(ticker: str) -> str:
19
  """
20
 
21
 
22
- url = f"https://finance.yahoo.com/quote/{ticker}"
23
- headers = {"User-Agent": "Mozilla/5.0"} # Avoid bot detection
24
- response = requests.get(url, headers=headers)
25
-
26
- if response.status_code != 200:
27
- return f"Failed to fetch data for {ticker}"
28
-
29
- soup = BeautifulSoup(response.text, "html.parser")
30
- price_span = soup.find("fin-streamer", {"data-field": "regularMarketPrice"})
31
 
32
  if price_span:
33
  return f"The current price of {ticker} is ${price_span.text}"
 
3
  import requests
4
  import pytz
5
  import yaml
6
+ import yfinance as yf
7
  from bs4 import BeautifulSoup
8
  from tools.final_answer import FinalAnswerTool
9
 
 
20
  """
21
 
22
 
23
+ try:
24
+ stock = yf.Ticker(ticker)
25
+ price = stock.info.get("regularMarketPrice", "N/A") # Ensure correct field
26
+ currency = stock.info.get("currency", "USD") # Get currency
27
+ return f"{currency} {price:,.2f}" if price != "N/A" else "Price unavailable"
28
+ except Exception as e:
29
+ return f"Error fetching price: {e}"
 
 
30
 
31
  if price_span:
32
  return f"The current price of {ticker} is ${price_span.text}"