Hi folks,
I’m struggling with a scrape script in Python. In the purpose of checking the date, I change the local parameter to fr_FR. But since then the script return an empty string if I test it with the developer tools.
I debug it with VSCode and the output is looks good.
So what I’m missing ?
import re
import json
from bs4 import BeautifulSoup
import requests
from datetime import datetime
import time
import locale
import contextlib
@contextlib.contextmanager
def setlocale(*args, **kw):
saved = locale.setlocale(locale.LC_TIME)
yield locale.setlocale(*args, **kw)
locale.setlocale(locale.LC_TIME, saved)
def get_c_locale_abbrev():
with setlocale(locale.LC_TIME, "C"):
return time.strftime("%a-%b")
def is_current_week(week_title,indice):
try:
dates = week_title.replace("Du ", "").split(" au ")
slitdate=dates[1].split(" ")
if indice == 2 :
dates[1]='27'
else:
dates[1]=slitdate[0]
dates.append(slitdate[1])
start_date = datetime.strptime(dates[0] + f" {dates[2]}" + f" {current_date.year}", "%d %B %Y")
end_date = datetime.strptime(dates[1] + f" {dates[2]}" + f" {current_date.year}", "%d %B %Y")
return start_date <= current_date <= end_date
except Exception as e:
return False
url="https://www.rosnysousbois.fr/education/restauration-scolaire/"
response = requests.get(url)
# Parse the HTML with BeautifulSoup
soup = BeautifulSoup(response.content, 'html.parser')
# Extract the current week
current_date = datetime.now()
# Extract week sections
week_sections = soup.find_all('div', class_='accordion__item')
menu = {}
locale.setlocale(locale.LC_TIME, 'fr_FR.utf8')
#print(time.strftime('%a-%b'))
#print(get_c_locale_abbrev())
#print(time.strftime('%a-%b'))
for indice,week in enumerate(week_sections, start=1):
week_title = week.find('button').get_text(strip=True)
# Check if the week matches the current week
if not is_current_week(week_title, indice):
continue
days = week.find('div', class_='accordion__item__content').find_all('h2')
week_data = {
"week": week_title,
"days": []
}
for day in days:
day_name = day.get_text(strip=True)
ul = day.find_next_sibling('ul')
items = [li.get_text(strip=True) for li in ul.find_all('li')]
goûter_tag = day.find_next_sibling('p' )
goûter = goûter_tag.get_text(strip=True).replace('Goûter :', '').strip() if goûter_tag else None
week_data["days"].append({
"day": day_name,
"menu": items,
"snack": goûter
})
menu['a'+str(len(menu.keys()))]=(week_data)
# Convert to JSON format
menu_json = json.dumps(menu, indent=4, ensure_ascii=False)
#print(locale.getlocale())
locale.setlocale(locale.LC_TIME, 'en_US.utf8')
#print(locale.getlocale())
# Print the resulting dictionary
print(menu_json)
exit()
If comment the line changing the format, the return isn’t empty.