from bs4 import BeautifulSoup import requests def get_excuse(): """ Fetches a random developer excuse from devexcuses.com Returns: str: A formatted excuse string wrapped in code block """ url = "http://www.devexcuses.com" response = requests.get(url, headers={"Cache-Control": "no-cache"}) response.raise_for_status() # Raise exception for bad responses soup = BeautifulSoup(response.content, features="html.parser") excuse_element = soup.find("p", {"class": "excuse"}) if not excuse_element or not excuse_element.contents: return "```Failed to get an excuse. Maybe that's your excuse?```" # Extract the excuse text more reliably using the tag inside the

excuse_link = excuse_element.find("a") excuse_text = ( excuse_link.text.strip() if excuse_link else excuse_element.text.strip() ) return f"```{excuse_text}```"