Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,23 +3,34 @@ import requests
|
|
3 |
from bs4 import BeautifulSoup
|
4 |
from urllib.parse import urljoin
|
5 |
|
6 |
-
def parse_links(ort):
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
|
|
|
|
23 |
|
24 |
# Erstelle die Gradio-Schnittstelle
|
25 |
with gr.Blocks() as demo:
|
@@ -31,5 +42,4 @@ with gr.Blocks() as demo:
|
|
31 |
# Verbinde den Button mit der Funktion
|
32 |
button.click(fn=parse_links, inputs=ort_input, outputs=links_output)
|
33 |
|
34 |
-
#
|
35 |
-
demo.launch()
|
|
|
3 |
from bs4 import BeautifulSoup
|
4 |
from urllib.parse import urljoin
|
5 |
|
6 |
+
def parse_links(ort):
|
7 |
+
try:
|
8 |
+
# Konstruiere die vollständige URL
|
9 |
+
initial_url = "http://spiegel.de"
|
10 |
+
|
11 |
+
# Senden der Anfrage an die initiale URL
|
12 |
+
response = requests.get(initial_url)
|
13 |
+
response.raise_for_status() # Überprüfen, ob die Anfrage erfolgreich war
|
14 |
+
|
15 |
+
# Parse the HTML content using BeautifulSoup
|
16 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
17 |
+
|
18 |
+
# Den body-Tag finden
|
19 |
+
body_tag = soup.find('body')
|
20 |
+
|
21 |
+
# Den Text des body-Tags zurückgeben
|
22 |
+
body_text = body_tag.get_text()
|
23 |
+
|
24 |
+
# Den Text ausgeben
|
25 |
+
print(body_text)
|
26 |
+
|
27 |
+
# Rückgabe als Dictionary, um es mit gr.JSON kompatibel zu machen
|
28 |
+
return {"body_text": body_text}
|
29 |
|
30 |
+
except requests.RequestException as e:
|
31 |
+
return {"error": str(e)}
|
32 |
+
except Exception as e:
|
33 |
+
return {"error": str(e)}
|
34 |
|
35 |
# Erstelle die Gradio-Schnittstelle
|
36 |
with gr.Blocks() as demo:
|
|
|
42 |
# Verbinde den Button mit der Funktion
|
43 |
button.click(fn=parse_links, inputs=ort_input, outputs=links_output)
|
44 |
|
45 |
+
#
|
|