>>> stock_price("EEEE") 'No tag with the specified data-testid attribute found.' >>> isinstance(float(stock_price("GOOG")),float) True
(symbol: str = "AAPL")
| 21 | |
| 22 | |
| 23 | def stock_price(symbol: str = "AAPL") -> str: |
| 24 | """ |
| 25 | >>> stock_price("EEEE") |
| 26 | 'No <fin-streamer> tag with the specified data-testid attribute found.' |
| 27 | >>> isinstance(float(stock_price("GOOG")),float) |
| 28 | True |
| 29 | """ |
| 30 | url = f"https://finance.yahoo.com/quote/{symbol}?p={symbol}" |
| 31 | yahoo_finance_source = httpx.get( |
| 32 | url, headers={"USER-AGENT": "Mozilla/5.0"}, timeout=10, follow_redirects=True |
| 33 | ).text |
| 34 | soup = BeautifulSoup(yahoo_finance_source, "html.parser") |
| 35 | |
| 36 | if specific_fin_streamer_tag := soup.find("span", {"data-testid": "qsp-price"}): |
| 37 | return specific_fin_streamer_tag.get_text() |
| 38 | return "No <fin-streamer> tag with the specified data-testid attribute found." |
| 39 | |
| 40 | |
| 41 | # Search for the symbol at https://finance.yahoo.com/lookup |
no test coverage detected