Spaces:
Running
Running
import gradio as gr | |
import requests | |
from bs4 import BeautifulSoup | |
from urllib.parse import urljoin | |
def parse_links_and_content(ort): | |
base_url = "https://vereine-in-deutschland.net" | |
# Konstruiere die vollständige URL | |
url = f"{base_url}/vereine/Bayern/{ort}" | |
try: | |
# Senden der Anfrage an die URL | |
response = requests.get(url) | |
response.raise_for_status() # Überprüfen, ob die Anfrage erfolgreich war | |
# Parse the HTML content using BeautifulSoup | |
soup = BeautifulSoup(response.content, 'html.parser') | |
# Finde das Element mit dem CSS-Selektor | |
target_div = soup.select_one('div.row-cols-1:nth-child(4)') | |
if target_div: | |
# Extrahiere alle Links aus dem Element und füge die Base URL hinzu | |
links = [urljoin(base_url, a['href']) for a in target_div.find_all('a', href=True)] | |
# Extrahiere den HTML-Code des Elements | |
html_code = str(target_div) | |
return html_code, links | |
else: | |
return "Target div not found", [] | |
except Exception as e: | |
return str(e), [] | |
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 results | |
# Erstelle die Gradio-Schnittstelle | |
with gr.Blocks() as demo: | |
gr.Markdown("# Vereine in Bayern Parser") | |
ort_input = gr.Textbox(label="Ort", placeholder="Gib den Namen des Ortes ein") | |
html_output = gr.Code(label="HTML-Code des Elements", language="html") | |
links_output = gr.JSON(label="Gefundene Links") | |
content_output = gr.JSON(label="Inhalt der Links") | |
def process_ort(ort): | |
html_code, links = parse_links_and_content(ort) | |
scraped_content = scrape_links(links) | |
return html_code, links, scraped_content | |
# Button zum Starten der Parsung | |
button = gr.Button("Parse und Scrape") | |
# Verbinde den Button mit der Funktion | |
button.click(fn=process_ort, inputs=ort_input, outputs=[html_output, links_output, content_output]) | |
# Starte die Gradio-Anwendung | |
demo.launch() |