import gradio as gr import requests from bs4 import BeautifulSoup import time def fetch_xanthi_weather_data(url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' } try: response = requests.get(url, headers=headers) response.raise_for_status() soup = BeautifulSoup(response.content, 'html.parser') data = {} # Extracting Latest Values (Handle potential None) date_time_span = soup.find_all('span')[10] data['last'] = date_time_span.text.strip() # Extracting Temperature (Handle potential None) temp_element = soup.find('span', string='Θερμοκρασία') if temp_element: temp_value = soup.find('div', class_='lright').find('span') data['temperature'] = temp_value.text.strip() else: data['temperature'] = 'N/A' # Extracting Humidity (Handle potential None) humidity_element = soup.find('span', string='Υγρασία') if humidity_element: humidity_div = humidity_element.find_next('div', class_='lright').find('span') data['humidity'] = humidity_div.text.strip() else: data['humidity'] = 'N/A' # Extracting Wind Speed (Handle potential None) wind_element = soup.find('span', string='Άνεμος') if wind_element: wind_div = wind_element.find_next('div', class_='lright').find('span') data['wind_speed'] = wind_div.text.strip() else: data['wind_speed'] = 'N/A' # Extracting Wind Direction (Handle potential None) sd_element = soup.find('span', string='Σημείο Δρόσου') if sd_element: sd_div = sd_element.find_next('div', class_='lright').find('span') data['sd'] = sd_div.text.strip() else: data['sd'] = 'N/A' # Extracting Pressure (Handle potential None) pressure_element = soup.find('span', string='Βαρόμετρο') if pressure_element: pressure_div = pressure_element.find_next('div', class_='lright').find('span') data['pressure'] = pressure_div.text.strip() else: data['pressure'] = 'N/A' # Extracting Rainfall (Handle potential None) rain_element = soup.find('span', string='Ραγδαιότητα') if rain_element: rain_div = rain_element.find_next('div', class_='lright').find('span') data['rainfall'] = rain_div.text.strip() else: data['rainfall'] = 'N/A' return data except requests.exceptions.RequestException as e: return {"error": f"Error fetching data: {e}"} except Exception as e: #Catch all other exceptions. return {"error": f"Error parsing data: {e}"} def display_weather(): url = 'https://penteli.meteo.gr/stations/xanthi/' weather_data = fetch_xanthi_weather_data(url) if "error" in weather_data: return weather_data["error"] output = f"""