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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -18
app.py CHANGED
@@ -5,32 +5,51 @@ from urllib.parse import urljoin
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,20 +76,19 @@ with gr.Blocks() as demo:
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()
 
5
 
6
  def parse_links_and_content(ort):
7
  base_url = "https://vereine-in-deutschland.net"
8
+ all_links = []
9
+
10
  # Konstruiere die vollständige URL
11
+ initial_url = f"{base_url}/vereine/Bayern/{ort}/p/1"
12
 
13
  try:
14
+ # Senden der Anfrage an die initiale URL
15
+ response = requests.get(initial_url)
16
  response.raise_for_status() # Überprüfen, ob die Anfrage erfolgreich war
17
 
18
  # Parse the HTML content using BeautifulSoup
19
  soup = BeautifulSoup(response.content, 'html.parser')
20
 
21
+ # Ermittle die letzte Seite
22
+ link_element = soup.select_one('li.page-item:nth-child(8) > a:nth-child(1)')
23
 
24
+ if link_element and 'href' in link_element.attrs:
25
+ href = link_element['href']
26
+ # Extrahiere die letzten beiden Zeichen der URL
27
+ last_two_chars = href[-2:]
 
 
28
 
29
+ # Konvertiere die letzten beiden Zeichen in einen Integer
30
+ last_two_chars_int = int(last_two_chars)
31
  else:
32
+ last_two_chars_int = 1 # Falls die letzte Seite nicht gefunden wird, nimm an, dass es nur eine Seite gibt
33
+
34
+ # Schleife durch alle Seiten und sammle Links
35
+ for page_number in range(1, last_two_chars_int + 1):
36
+ page_url = f"{base_url}/vereine/Bayern/{ort}/p/{page_number}"
37
+ response = requests.get(page_url)
38
+ response.raise_for_status()
39
+
40
+ soup = BeautifulSoup(response.content, 'html.parser')
41
+ target_div = soup.select_one('div.row-cols-1:nth-child(4)')
42
+
43
+ if target_div:
44
+ links = [urljoin(base_url, a['href']) for a in target_div.find_all('a', href=True)]
45
+ all_links.extend(links)
46
+ else:
47
+ print(f"Target div not found on page {page_number}")
48
+
49
  except Exception as e:
50
  return str(e), []
51
+
52
+ return all_links
53
 
54
  def scrape_links(links):
55
  results = []
 
76
  gr.Markdown("# Vereine in Bayern Parser")
77
 
78
  ort_input = gr.Textbox(label="Ort", placeholder="Gib den Namen des Ortes ein")
 
79
  links_output = gr.JSON(label="Gefundene Links")
80
  content_output = gr.JSON(label="Inhalt der Links")
81
 
82
  def process_ort(ort):
83
+ links = parse_links_and_content(ort)
84
  scraped_content = scrape_links(links)
85
+ return links, scraped_content
86
 
87
  # Button zum Starten der Parsung
88
  button = gr.Button("Parse und Scrape")
89
 
90
  # Verbinde den Button mit der Funktion
91
+ button.click(fn=process_ort, inputs=ort_input, outputs=[links_output, content_output])
92
 
93
  # Starte die Gradio-Anwendung
94
+ demo.launch()