-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu2html.py
More file actions
76 lines (62 loc) · 3.19 KB
/
menu2html.py
File metadata and controls
76 lines (62 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import requests
from bs4 import BeautifulSoup
from datetime import datetime, timedelta
import locale
# Set the locale to Dutch
locale.setlocale(locale.LC_TIME, 'nl_NL.UTF-8')
# Function to check if a given date is a Monday, Tuesday, Thursday, or Friday
def is_valid_day(date):
return date.weekday() in [0, 1, 3, 4]
# The URL of the page
url = "https://order.hanssens.be/menu/C32A/GBS-Melle-De-Parkschool"
# Send a GET request to the URL
response = requests.get(url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Parse the HTML content of the page
soup = BeautifulSoup(response.content, 'html.parser')
# Create HTML content
html_content = "<!DOCTYPE html><html><head><title>Weekmenu De Parkschool</title><link rel='stylesheet' type='text/css' href='style.css'></head><body><br>"
# Get current date
current_date_obj = datetime.now()
# Iterate over the next 5 dates (including the current date)
for i in range(5):
next_date = current_date_obj + timedelta(days=i)
if is_valid_day(next_date):
# Find the day element for the current date
day_element = soup.find("div", class_="day", attrs={"data-mob-scroll-target": next_date.strftime("%d-%m-%Y")})
if day_element:
# Extract the day of the week and date
day_info = day_element.find("div", class_="day__header--info")
day_of_week = day_info.find("div", class_="day__header--day").text.strip()
date_text = day_info.find("div", class_="day__header--date").text.strip()
# Construct the date with the current year
header_date = datetime.strptime(f"{date_text} {current_date_obj.year}", "%d %B %Y")
# Extract the menu items for soup and menu
soup_items = day_element.find("div", class_="day__content--soup")
menu_items = day_element.find("div", class_="day__content--menu")
# Add date as heading
html_content += f"<h1>{day_of_week}, {header_date.strftime('%d %B %Y')}</h1>"
# Add menu items or "Geen School" if no items are found
if soup_items or menu_items:
html_content += "<ul>"
if soup_items:
for item in soup_items.find_all("div", class_="day__content--item"):
html_content += f"<li><h2>{item.text}</h2></li>"
if menu_items:
for item in menu_items.find_all("div", class_="day__content--item"):
html_content += f"<li><h2>{item.text}</h2></li>"
html_content += "</ul>"
else:
html_content += "<h2>Geen Middagmaal</h2>"
# Close HTML content
html_content += "</body></html>"
# Save the HTML output to a file
try:
with open("index.html", "w", encoding="utf-8") as file:
file.write(html_content)
print("HTML file saved as index.html")
except Exception as e:
print("Error:", e)
else:
print("Failed to retrieve the menu.")