from bs4 import BeautifulSoup
from urllib.request import urlopen
from json import dumps
url = "https://rozklad.ztu.edu.ua/schedule/group/%D0%86%D0%9F%D0%97-21-3?new"
bs=BeautifulSoup(urlopen(url).read(), features="lxml")
schedule = []
schedule_table = bs.find("table", attrs={"class":"schedule"})
for tr in schedule_table.findAll("tr"):
if str(tr.th.text).strip() == "":
continue
tds = tr.findAll("td")
for td in tds:
newitem = {
"day": td.get("day")[0:-2],
"hour": td.get("hour")
}
if len(td.get("class")) > 0:
newitem.update({
"content": {
"variative": {
"subject": td.find("div", attrs={"class":"subject"}).text.strip(),
"room": td.find("span", attrs={"class":"room"}).parent.text.strip(),
"room_span": td.find("span", attrs={"class":"room"}).text.strip(),
"room_changed": "changed" in td.find("span", attrs={"class":"room"}).get("class"),
"teacher": td.find("div", attrs={"class":"teacher"}).text.strip(),
}
}
})
schedule.append(newitem)
print(dumps(schedule, indent = 4, ensure_ascii = False))
# так можно фильтровать
wednesdays = [d for d in schedule if d["day"] == "Середа"]
print(dumps(wednesdays, indent = 4, ensure_ascii = False))
# и так по любому из параметров