bsenst commited on
Commit
efb0e4e
·
verified ·
1 Parent(s): 1a56b6b

add html file upload option

Browse files
Files changed (1) hide show
  1. app.py +52 -14
app.py CHANGED
@@ -2,8 +2,8 @@ import streamlit as st
2
  import requests
3
  from bs4 import BeautifulSoup
4
 
5
- # Funktion, um alle URLs von einer Webseite zu extrahieren
6
- def extract_urls(url):
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 str(e)
 
 
 
 
 
 
 
 
 
15
 
16
  # Streamlit App
17
  st.title("Webseiten-URL-Extraktor")
18
 
19
- # Eingabefeld für die URL
20
- url_input = st.text_input("Gib die URL der Webseite ein:", placeholder="https://example.com")
21
-
22
- # Wenn der Nutzer eine URL eingibt und auf den Button klickt
23
- if st.button("URLs extrahieren"):
24
- if url_input:
25
- st.write(f"Extrahiere URLs von: {url_input}")
26
- urls = extract_urls(url_input)
27
- for url in set(urls):
28
- if url.startswith("http"):
29
- st.write(url)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.")