add html file upload option
Browse files
app.py
CHANGED
@@ -2,8 +2,8 @@ import streamlit as st
|
|
2 |
import requests
|
3 |
from bs4 import BeautifulSoup
|
4 |
|
5 |
-
#
|
6 |
-
def
|
7 |
try:
|
8 |
response = requests.get(url)
|
9 |
response.raise_for_status()
|
@@ -11,19 +11,57 @@ def extract_urls(url):
|
|
11 |
links = [a.get('href') for a in soup.find_all('a', href=True)]
|
12 |
return links
|
13 |
except Exception as e:
|
14 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
# Streamlit App
|
17 |
st.title("Webseiten-URL-Extraktor")
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import requests
|
3 |
from bs4 import BeautifulSoup
|
4 |
|
5 |
+
# Function to extract all URLs from a webpage
|
6 |
+
def extract_urls_from_url(url):
|
7 |
try:
|
8 |
response = requests.get(url)
|
9 |
response.raise_for_status()
|
|
|
11 |
links = [a.get('href') for a in soup.find_all('a', href=True)]
|
12 |
return links
|
13 |
except Exception as e:
|
14 |
+
return f"Error processing the URL: {e}"
|
15 |
+
|
16 |
+
# Function to extract all URLs from uploaded HTML content
|
17 |
+
def extract_urls_from_html(html_content):
|
18 |
+
try:
|
19 |
+
soup = BeautifulSoup(html_content, 'html.parser')
|
20 |
+
links = [a.get('href') for a in soup.find_all('a', href=True)]
|
21 |
+
return links
|
22 |
+
except Exception as e:
|
23 |
+
return f"Error processing the HTML file: {e}"
|
24 |
|
25 |
# Streamlit App
|
26 |
st.title("Webseiten-URL-Extraktor")
|
27 |
|
28 |
+
# Input options: URL or HTML file
|
29 |
+
input_option = st.radio("Wählen Sie die Eingabemethode:", ("URL", "HTML-Datei hochladen"))
|
30 |
+
|
31 |
+
if input_option == "URL":
|
32 |
+
# Input field for URL
|
33 |
+
url_input = st.text_input("Gib die URL der Webseite ein:", placeholder="https://example.com")
|
34 |
+
|
35 |
+
if st.button("URLs extrahieren"):
|
36 |
+
if url_input:
|
37 |
+
st.write(f"Extrahiere URLs von: {url_input}")
|
38 |
+
urls = extract_urls_from_url(url_input)
|
39 |
+
if isinstance(urls, list):
|
40 |
+
for url in set(urls):
|
41 |
+
if url.startswith("http"):
|
42 |
+
st.write(url)
|
43 |
+
else:
|
44 |
+
st.error(urls)
|
45 |
+
else:
|
46 |
+
st.warning("Bitte geben Sie eine gültige URL ein.")
|
47 |
+
|
48 |
+
elif input_option == "HTML-Datei hochladen":
|
49 |
+
# File uploader for HTML files
|
50 |
+
uploaded_file = st.file_uploader("Laden Sie eine HTML-Datei hoch:", type="html")
|
51 |
+
|
52 |
+
if st.button("URLs extrahieren"):
|
53 |
+
if uploaded_file:
|
54 |
+
try:
|
55 |
+
html_content = uploaded_file.read().decode("utf-8")
|
56 |
+
st.write("Extrahiere URLs aus der hochgeladenen HTML-Datei...")
|
57 |
+
urls = extract_urls_from_html(html_content)
|
58 |
+
if isinstance(urls, list):
|
59 |
+
for url in set(urls):
|
60 |
+
if url.startswith("http"):
|
61 |
+
st.write(url)
|
62 |
+
else:
|
63 |
+
st.error(urls)
|
64 |
+
except Exception as e:
|
65 |
+
st.error(f"Fehler beim Verarbeiten der HTML-Datei: {e}")
|
66 |
+
else:
|
67 |
+
st.warning("Bitte laden Sie eine HTML-Datei hoch.")
|