'Printing out the entire Table Row of one or more found elements

This is my code so far:

from bs4 import BeautifulSoup
import requests
import re

day = input("Für welchen tag willst du den vertretungsplan sehen? \n Heute = 1, Morgen = 2, Übermorgen = 3 \n")
if day == 1 or 2 or 3:
    pass
else:
    print("Bitte gebe eine zahl von 1-3 ein!")

url = f"https://jenaplan-weimar.de/vplan/Vertretungsplaene/SchuelerInnen/subst_00{day}.htm"
result = requests.get(url)
doc = BeautifulSoup(result.text, "html.parser")
wunschklasse = input("Für welche Klasse möchtest du den Vertretungsplan sehen? (Achtung, bitte beachte die Abkürzungen!) ")
klasse = doc.find_all(text=re.compile(f"{wunschklasse}.*"))
# parent = klasse[0].parent
# parent2 = parent.parent
# parent3 = parent2.parent

if klasse == []:
    print("Sry aber deine gewählte Klasse hat keine Vertertung bzw keinen Ausfall")
else:
    print(f"Für {wunschklasse} wurde folgendes gefunden: {klasse}")

As you can see, I let the user gives me a value, that then is being searched in the table. So far, all found values are being printed out, but I also want to print out the entire table row where the Value is standing in. In the end, it should give me all aviable information to the value from the user. Thank you for your help!



Solution 1:[1]

You could first get all rows, and later run for-loop to check every row separatelly. And when you find text in row then you have all row and you don't have to search parents.


For test I assigned some (random?) values instead of input()

In BeautifulSoup I used response.content instead of response.text to get correctly converted native chars. (with .text I got lösen instead of lösen)

import requests
from bs4 import BeautifulSoup
import re

#day = input("Für welchen tag willst du den vertretungsplan sehen? \n Heute = 1, Morgen = 2, Übermorgen = 3 \n")
day = "1"

if day not in ("1", "2", "3"):
    print("Bitte gebe eine zahl von 1-3 ein!")
else:
       
    url = f"https://jenaplan-weimar.de/vplan/Vertretungsplaene/SchuelerInnen/subst_00{day}.htm"
    
    response = requests.get(url)
    doc = BeautifulSoup(response.content, "html.parser")
    
    #wunschklasse = input("Für welche Klasse möchtest du den Vertretungsplan sehen? (Achtung, bitte beachte die Abkürzungen!) ")
    wunschklasse = "STG"

    found = False

    all_rows = doc.find_all('tr')

    for row in all_rows:

        # search in row
        klasse = row.find_all(text=re.compile(f"{wunschklasse}.*"))

        if klasse:
            row_text = row.get_text(strip=True, separator='|')
            print(f"Für {wunschklasse} wurde folgendes gefunden: {row_text}")

            found = True

    # ----
    
    if not found:
        print("Sry aber deine gewählte Klasse hat keine Vertertung bzw keinen Ausfall")

Result:

Für STG wurde folgendes gefunden: 3|10:10-10:55|Aff|STG|RG-Aff|Vertret.
Für STG wurde folgendes gefunden: 4|11:00-11:45|Aff|STG|RG-Aff|Vertret.
Für STG wurde folgendes gefunden: 7|14:15-15:00|Elc|STG|---|Ausfall
Für STG wurde folgendes gefunden: 6 - 7|13:05-15:00|Löw|STG|---|Ausfall|Bitte Aufgaben in der Schulcloud lösen

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1