|
|
import gradio as gr |
|
|
import requests |
|
|
from bs4 import BeautifulSoup |
|
|
|
|
|
|
|
|
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 <body> tags. |
|
|
|
|
|
Args: |
|
|
destination (str): The destination city for the train connection search. |
|
|
|
|
|
Returns: |
|
|
str: The text content of the <body> 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} |
|
|
|
|
|
|
|
|
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, params=params, headers=headers, timeout=10) |
|
|
response.raise_for_status() |
|
|
|
|
|
|
|
|
soup = BeautifulSoup(response.text, 'html.parser') |
|
|
body = soup.find('body') |
|
|
|
|
|
if body: |
|
|
|
|
|
return body.get_text(separator=' ', strip=True) |
|
|
else: |
|
|
return "Error: Could not find the <body> tag in the response." |
|
|
|
|
|
except requests.exceptions.RequestException as e: |
|
|
return f"Error during request: {e}" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
), |
|
|
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" |
|
|
) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |