Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
from bs4 import BeautifulSoup
|
4 |
|
5 |
-
def
|
6 |
# Konstruiere die vollständige URL
|
7 |
url = f"https://vereine-in-deutschland.net/vereine/Bayern/{ort}"
|
8 |
|
@@ -14,34 +14,61 @@ def parse_link(ort):
|
|
14 |
# Parse the HTML content using BeautifulSoup
|
15 |
soup = BeautifulSoup(response.content, 'html.parser')
|
16 |
|
17 |
-
# Finde
|
18 |
-
|
19 |
|
20 |
-
if
|
21 |
-
|
22 |
-
|
23 |
-
last_two_chars = href[-2:]
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
|
28 |
-
return
|
29 |
else:
|
30 |
-
return "
|
31 |
except Exception as e:
|
32 |
-
return str(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
# Erstelle die Gradio-Schnittstelle
|
35 |
with gr.Blocks() as demo:
|
36 |
gr.Markdown("# Vereine in Bayern Parser")
|
37 |
-
ort_input = gr.Textbox(label="Ort", placeholder="Gib den Namen des Ortes ein")
|
38 |
-
output = gr.Number(label="Letzte beiden Zeichen der URL (als Integer)")
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
# Button zum Starten der Parsung
|
41 |
-
button = gr.Button("Parse
|
42 |
|
43 |
# Verbinde den Button mit der Funktion
|
44 |
-
button.click(fn=
|
45 |
|
46 |
# Starte die Gradio-Anwendung
|
47 |
demo.launch()
|
|
|
2 |
import requests
|
3 |
from bs4 import BeautifulSoup
|
4 |
|
5 |
+
def parse_links_and_content(ort):
|
6 |
# Konstruiere die vollständige URL
|
7 |
url = f"https://vereine-in-deutschland.net/vereine/Bayern/{ort}"
|
8 |
|
|
|
14 |
# Parse the HTML content using BeautifulSoup
|
15 |
soup = BeautifulSoup(response.content, 'html.parser')
|
16 |
|
17 |
+
# Finde das Element mit dem CSS-Selektor
|
18 |
+
target_div = soup.select_one('div.row-cols-1:nth-child(4)')
|
19 |
|
20 |
+
if target_div:
|
21 |
+
# Extrahiere alle Links aus dem Element
|
22 |
+
links = [a['href'] for a in target_div.find_all('a', href=True)]
|
|
|
23 |
|
24 |
+
# Extrahiere den HTML-Code des Elements
|
25 |
+
html_code = str(target_div)
|
26 |
|
27 |
+
return html_code, links
|
28 |
else:
|
29 |
+
return "Target div not found", []
|
30 |
except Exception as e:
|
31 |
+
return str(e), []
|
32 |
+
|
33 |
+
def scrape_links(links):
|
34 |
+
results = []
|
35 |
+
for link in links:
|
36 |
+
try:
|
37 |
+
# Senden der Anfrage an die URL
|
38 |
+
response = requests.get(link)
|
39 |
+
response.raise_for_status() # Überprüfen, ob die Anfrage erfolgreich war
|
40 |
+
|
41 |
+
# Parse the HTML content using BeautifulSoup
|
42 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
43 |
+
|
44 |
+
# Extrahiere den gewünschten Inhalt (hier als Beispiel der Titel der Seite)
|
45 |
+
content = soup.title.string if soup.title else "No title found"
|
46 |
+
|
47 |
+
results.append((link, content))
|
48 |
+
except Exception as e:
|
49 |
+
results.append((link, str(e)))
|
50 |
+
|
51 |
+
return results
|
52 |
|
53 |
# Erstelle die Gradio-Schnittstelle
|
54 |
with gr.Blocks() as demo:
|
55 |
gr.Markdown("# Vereine in Bayern Parser")
|
|
|
|
|
56 |
|
57 |
+
ort_input = gr.Textbox(label="Ort", placeholder="Gib den Namen des Ortes ein")
|
58 |
+
html_output = gr.Code(label="HTML-Code des Elements", language="html")
|
59 |
+
links_output = gr.JSON(label="Gefundene Links")
|
60 |
+
content_output = gr.JSON(label="Inhalt der Links")
|
61 |
+
|
62 |
+
def process_ort(ort):
|
63 |
+
html_code, links = parse_links_and_content(ort)
|
64 |
+
scraped_content = scrape_links(links)
|
65 |
+
return html_code, links, scraped_content
|
66 |
+
|
67 |
# Button zum Starten der Parsung
|
68 |
+
button = gr.Button("Parse und Scrape")
|
69 |
|
70 |
# Verbinde den Button mit der Funktion
|
71 |
+
button.click(fn=process_ort, inputs=ort_input, outputs=[html_output, links_output, content_output])
|
72 |
|
73 |
# Starte die Gradio-Anwendung
|
74 |
demo.launch()
|