import gradio as gr import requests from bs4 import BeautifulSoup # --- Core Function (from the previous answer) --- def get_search_body_text(destination: str) -> str: """ Performs a Google search for a train connection using requests and BeautifulSoup. This function fetches the search results page, parses the HTML, and returns all the text content found within the tags. Args: destination (str): The destination city for the train connection search. Returns: str: The text content of the tag of the search results page, or an error message if the request fails or the body is not found. """ if not destination or not destination.strip(): return "Please enter a destination city." query = f"zugverbindung bad kissingen {destination}" url = "https://www.google.com/search" params = {'q': query} # Set a User-Agent header to mimic a real browser 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: # Make the HTTP GET request response = requests.get(url, params=params, headers=headers, timeout=10) response.raise_for_status() # Raise an exception for bad status codes # Parse the HTML content soup = BeautifulSoup(response.text, 'html.parser') body = soup.find('body') if body: # Extract all text, separated by spaces, with extra whitespace stripped return body.get_text(separator=' ', strip=True) else: return "Error: Could not find the tag in the response." except requests.exceptions.RequestException as e: return f"Error during request: {e}" # --- Gradio Interface Definition --- # Create the Gradio interface by wrapping the function demo = gr.Interface( fn=get_search_body_text, inputs=gr.Textbox( label="Destination City", placeholder="e.g., Würzburg, Frankfurt am Main, Nürnberg..." ), outputs=gr.Textbox( label="Raw Body Content from Google Search", lines=20 # Give the output box a good height ), title="Train Connection Search", description="Enter a destination city to search for train connections from **Bad Kissingen**. The app fetches the Google search results and displays the raw text content of the page body. Note: The output is unformatted text and can be very long.", examples=[ ["Würzburg"], ["Frankfurt am Main"], ["Nürnberg"] ], allow_flagging="never" ) # --- Launch the App --- if __name__ == "__main__": demo.launch()