support:beautifulsoup_installationsanleitung
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
| support:beautifulsoup_installationsanleitung [2025/07/14 18:53] – admin | support:beautifulsoup_installationsanleitung [2025/08/10 19:42] (aktuell) – admin | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| - | ====== BeautifulSoup Installationsanleitung ====== | + | **<< |
| - | {{: | + | ====== BeautifulSoup Installationsanleitung |
| + | {{: | ||
| ===== BeautifulSoup installieren ===== | ===== BeautifulSoup installieren ===== | ||
| - | @sudo apt-get install python3-bs4 | + | @sudo apt-get install python3-bs4 |
| - | @sudo apt-get install python3-lxml | + | |
| - | @sudo apt-get install python3-html5lib | + | |
| ===== BeautifulSoup Script Beispiele ===== | ===== BeautifulSoup Script Beispiele ===== | ||
| - | Datei Beispielscript.py erstellen und Inhalt einfügen und speichern. Scrip wird dann folgendermaßen ausgeführt, | + | Datei Beispielscript.py erstellen und Inhalt einfügen und speichern. Scrip wird dann folgendermaßen ausgeführt, |
| - | @python3 Beispielscript.py | + | @python3 Beispielscript.py |
| ===== Alle Webseiten werden ausgelesen inkl. Inhalte und im Terminal angezeigt ===== | ===== Alle Webseiten werden ausgelesen inkl. Inhalte und im Terminal angezeigt ===== | ||
| | | ||
| - | from bs4 import BeautifulSoup | + | from bs4 import BeautifulSoup |
| - | import time | + | |
| + | |||
| + | def get_current_date(): | ||
| + | | ||
| + | |||
| + | def get_current_time(): | ||
| + | | ||
| + | |||
| + | def scrape_website(url): | ||
| + | | ||
| + | soup = BeautifulSoup(response.text, | ||
| + | |||
| + | # Extract all text content from the website | ||
| + | | ||
| + | |||
| + | | ||
| + | |||
| + | if __name__ == " | ||
| + | | ||
| + | | ||
| + | |||
| + | | ||
| + | | ||
| + | |||
| + | # Specify the website URL to scrape | ||
| + | url = " | ||
| + | | ||
| - | def get_current_date(): | + | ===== Alle Webseiten werden ausgelesen inkl. Inhalte, im Terminal angezeigt und in Textdatei gespeichert. ===== |
| - | return time.strftime(" | + | |
| - | def get_current_time(): | + | |
| - | return f"The current time is {time.strftime(' | + | from bs4 import BeautifulSoup |
| + | | ||
| + | |||
| + | def get_current_date(): | ||
| + | | ||
| + | |||
| + | def get_current_time(): | ||
| + | | ||
| + | |||
| + | def scrape_website(url): | ||
| + | | ||
| + | soup = BeautifulSoup(response.text, | ||
| + | |||
| + | # Extract all text content from the website | ||
| + | | ||
| + | |||
| + | | ||
| + | |||
| + | with open(" | ||
| + | | ||
| + | |||
| + | if __name__ == " | ||
| + | | ||
| + | | ||
| + | |||
| + | | ||
| + | | ||
| + | |||
| + | # Specify the website URL to scrape | ||
| + | url = " | ||
| + | | ||
| - | def scrape_website(url): | + | ===== Alle Webseite und Inhalte auslesen und in Textdatei speichern ===== |
| - | response | + | |
| - | soup = BeautifulSoup(response.text, | + | |
| - | + | ||
| - | # Extract all text content from the website | + | |
| - | text_content | + | |
| - | + | ||
| - | print(" | + | |
| - | if __name__ == " | + | |
| - | start_date = get_current_date() | + | from bs4 import BeautifulSoup |
| - | start_time = get_current_time() | + | |
| - | + | ||
| - | print(" | + | def get_current_date(): |
| - | print(" | + | |
| - | + | ||
| - | # Specify the website URL to scrape | + | def get_current_time(): |
| - | url = " | + | |
| - | scrape_website(url) | + | |
| + | def scrape_website(url): | ||
| + | | ||
| + | soup = BeautifulSoup(response.text, | ||
| + | |||
| + | # Extract all text content from the website | ||
| + | | ||
| + | |||
| + | with open(" | ||
| + | | ||
| + | |||
| + | if __name__ == " | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| - | Alle Webseiten | + | ===== Alle Webseiten |
| - | import requests | + | |
| - | from bs4 import BeautifulSoup | + | |
| - | import time | + | |
| - | def get_current_date(): | + | import requests |
| - | return time.strftime(" | + | from bs4 import BeautifulSoup |
| - | + | from urllib.parse import urljoin | |
| - | def get_current_time(): | + | |
| - | | + | # Ersetzen Sie dies durch die URL der Webseite, die Sie scrapen möchten |
| - | + | url = ' | |
| - | def scrape_website(url): | + | |
| - | response = requests.get(url) | + | # Erstellen Sie eine Anfrage an die Webseite |
| - | | + | |
| - | + | | |
| - | # Extract all text content from the website | + | # Stellen Sie sicher, dass die Anfrage erfolgreich war |
| - | | + | if response.status_code == 200: |
| - | + | # Erzeugen Sie ein BeautifulSoup-Objekt | |
| - | print(" | + | soup = BeautifulSoup(response.text, |
| - | + | ||
| - | | + | # Drucken Sie die URL der Hauptseite |
| - | file.write(text_content) | + | |
| - | + | ||
| - | if __name__ == "__main__": | + | # Finden Sie alle Links auf der Hauptseite |
| - | start_date = get_current_date() | + | for link in soup.find_all('a', |
| - | | + | # Erstellen Sie eine vollständige URL für jeden Link |
| - | + | complete_url = urljoin(url, link[' | |
| - | print("Tool ' | + | |
| - | print(" | + | # Erstellen Sie eine Anfrage an jede Seite |
| - | + | response = requests.get(complete_url) | |
| - | # Specify the website URL to scrape | + | |
| - | url = "https://www.beispielwebseite.com" | + | # Stellen Sie sicher, dass die Anfrage erfolgreich war |
| - | scrape_website(url) | + | if response.status_code == 200: |
| + | # Erzeugen Sie ein BeautifulSoup-Objekt für die Seite | ||
| + | | ||
| + | | ||
| + | | ||
| + | print(f"URL der Seite: {complete_url}") | ||
| + | | ||
| + | # Drucken Sie das gesamte HTML-Inhalt der Seite | ||
| + | print(soup_page.prettify()) | ||
| + | | ||
| + | | ||
| + | file.write(soup_page.prettify() + "\n\n") | ||
| + | else: | ||
| + | print(f"Fehler bei der Anfrage der Seite: {response.status_code}") | ||
| + | else: | ||
| + | print(f"Fehler bei der Anfrage der Hauptseite: {response.status_code}") | ||
support/beautifulsoup_installationsanleitung.1752511986.txt.gz · Zuletzt geändert: von admin
