mgokg commited on
Commit
e410dd0
·
verified ·
1 Parent(s): a225c2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -36
app.py CHANGED
@@ -5,41 +5,32 @@ from urllib.parse import urljoin
5
 
6
  def parse_links_and_content(ort):
7
  base_url = "https://vereine-in-deutschland.net"
8
- all_links = []
 
9
 
10
- # Start with the first page
11
- page_number = 1
12
- while True:
13
- # Konstruiere die vollständige URL für die aktuelle Seite
14
- url = f"{base_url}/vereine/Bayern/{ort}/p/{page_number}"
15
 
16
- try:
17
- # Senden der Anfrage an die URL
18
- response = requests.get(url)
19
- response.raise_for_status() # Überprüfen, ob die Anfrage erfolgreich war
20
-
21
- # Parse the HTML content using BeautifulSoup
22
- soup = BeautifulSoup(response.content, 'html.parser')
23
-
24
- if page_number < last_two_chars_int:
25
-
26
- # Finde das Element mit dem CSS-Selektor
27
- target_div = soup.select_one('div.row-cols-1:nth-child(4)')
28
 
29
- if target_div:
30
- # Extrahiere alle Links aus dem Element und füge die Base URL hinzu
31
- links = [urljoin(base_url, a['href']) for a in target_div.find_all('a', href=True)]
32
- all_links.extend(links)
33
- else:
34
- print(f"Target div not found on page {page_number}")
35
 
36
- # Gehe zur nächsten Seite
37
- page_number += 1
38
- except Exception as e:
39
- print(f"Error on page {page_number}: {str(e)}")
40
- break
41
-
42
- return all_links
43
 
44
  def scrape_links(links):
45
  results = []
@@ -66,19 +57,20 @@ with gr.Blocks() as demo:
66
  gr.Markdown("# Vereine in Bayern Parser")
67
 
68
  ort_input = gr.Textbox(label="Ort", placeholder="Gib den Namen des Ortes ein")
 
69
  links_output = gr.JSON(label="Gefundene Links")
70
  content_output = gr.JSON(label="Inhalt der Links")
71
 
72
  def process_ort(ort):
73
- all_links = parse_links_and_content(ort)
74
- scraped_content = scrape_links(all_links)
75
- return all_links, scraped_content
76
 
77
  # Button zum Starten der Parsung
78
  button = gr.Button("Parse und Scrape")
79
 
80
  # Verbinde den Button mit der Funktion
81
- button.click(fn=process_ort, inputs=ort_input, outputs=[links_output, content_output])
82
 
83
  # Starte die Gradio-Anwendung
84
- demo.launch()
 
5
 
6
  def parse_links_and_content(ort):
7
  base_url = "https://vereine-in-deutschland.net"
8
+ # Konstruiere die vollständige URL
9
+ url = f"{base_url}/vereine/Bayern/{ort}"
10
 
11
+ try:
12
+ # Senden der Anfrage an die URL
13
+ response = requests.get(url)
14
+ response.raise_for_status() # Überprüfen, ob die Anfrage erfolgreich war
 
15
 
16
+ # Parse the HTML content using BeautifulSoup
17
+ soup = BeautifulSoup(response.content, 'html.parser')
18
+
19
+ # Finde das Element mit dem CSS-Selektor
20
+ target_div = soup.select_one('div.row-cols-1:nth-child(4)')
21
+
22
+ if target_div:
23
+ # Extrahiere alle Links aus dem Element und füge die Base URL hinzu
24
+ links = [urljoin(base_url, a['href']) for a in target_div.find_all('a', href=True)]
 
 
 
25
 
26
+ # Extrahiere den HTML-Code des Elements
27
+ html_code = str(target_div)
 
 
 
 
28
 
29
+ return html_code, links
30
+ else:
31
+ return "Target div not found", []
32
+ except Exception as e:
33
+ return str(e), []
 
 
34
 
35
  def scrape_links(links):
36
  results = []
 
57
  gr.Markdown("# Vereine in Bayern Parser")
58
 
59
  ort_input = gr.Textbox(label="Ort", placeholder="Gib den Namen des Ortes ein")
60
+ html_output = gr.Code(label="HTML-Code des Elements", language="html")
61
  links_output = gr.JSON(label="Gefundene Links")
62
  content_output = gr.JSON(label="Inhalt der Links")
63
 
64
  def process_ort(ort):
65
+ html_code, links = parse_links_and_content(ort)
66
+ scraped_content = scrape_links(links)
67
+ return html_code, links, scraped_content
68
 
69
  # Button zum Starten der Parsung
70
  button = gr.Button("Parse und Scrape")
71
 
72
  # Verbinde den Button mit der Funktion
73
+ button.click(fn=process_ort, inputs=ort_input, outputs=[html_output, links_output, content_output])
74
 
75
  # Starte die Gradio-Anwendung
76
+ demo.launch()