Spaces:
Running
Running
import gradio as gr | |
import requests | |
from bs4 import BeautifulSoup | |
from urllib.parse import urljoin | |
def parse_links(ort): | |
# Konstruiere die vollständige URL | |
initial_url = f"http://specialist-it.de:3000?q={ort}" | |
try: | |
# Senden der Anfrage an die initiale URL | |
response = requests.get(initial_url) | |
response.raise_for_status() # Überprüfen, ob die Anfrage erfolgreich war | |
print(response) | |
# Parse the HTML content using BeautifulSoup | |
soup = BeautifulSoup(response.content, 'html.parser') | |
def scrape_links(links): | |
results = [] | |
for link in links: | |
try: | |
# Senden der Anfrage an die URL | |
response = requests.get(link) | |
response.raise_for_status() # Überprüfen, ob die Anfrage erfolgreich war | |
# Parse the HTML content using BeautifulSoup | |
soup = BeautifulSoup(response.content, 'html.parser') | |
# Extrahiere den gewünschten Inhalt (hier als Beispiel der Titel der Seite) | |
content = soup.title.string if soup.title else "No title found" | |
results.append((link, content)) | |
except Exception as e: | |
results.append((link, str(e))) | |
return response | |
# Erstelle die Gradio-Schnittstelle | |
with gr.Blocks() as demo: | |
gr.Markdown("# Vereine in Bayern") | |
ort_input = gr.Textbox(label="Ort", placeholder="Gib den Namen des Ortes ein") | |
links_output = gr.JSON(label="Gefundene Vereine") | |
# Button zum Starten der Parsung | |
button = gr.Button("Parse und Scrape") | |
# Verbinde den Button mit der Funktion | |
button.click(fn=parse_links, inputs=ort_input, outputs=links_output) | |
# Starte die Gradio-Anwendung | |
demo.launch() | |