import os import requests from bs4 import BeautifulSoup from datetime import datetime # URL of the website with the menu links url = 'https://www.SCHOOL.org/departments/food-services/menus' # Function to fetch the page and parse the links def fetch_menu(url, target_school='Elementary', target_month='April', target_type='lunch'): response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') # Find all <a> tags with the data-file-name attribute links = soup.find_all('a', {'data-file-name': True}) # Filter the links to match the target school, month, and type (lunch or breakfast) for link in links: file_name = link['data-file-name'] if target_school.lower() in file_name.lower() and target_month.lower() in file_name.lower() and target_type.lower() in file_name.lower(): # Construct the full URL to the PDF pdf_url = 'https://www.SCHOOL.org' + link['href'] return pdf_url, file_name return None, None # Function to download the PDF def download_pdf(pdf_url, file_name, save_as='menu.pdf'): if not pdf_url: print(f"No menu found for {target_school} in {target_month}") return response = requests.get(pdf_url, stream=True) if response.status_code == 200: with open(save_as, 'wb') as f: for chunk in response.iter_content(chunk_size=8192): f.write(chunk) print(f"Downloaded {file_name} as {save_as}") else: print("Failed to download the file") # Main function to fetch and download the menu def main(): # Get the current month in a format like 'April', 'March', etc. current_month = datetime.now().strftime('%B') # 'April' for example # Set your target school target_school = 'Elementary' # Change this to 'Middle' or 'High' for other menus # Fetch the menu URL pdf_url, file_name = fetch_menu(url, target_school='Elementary', target_month=current_month, target_type='lunch') # Download the PDF download_pdf(pdf_url, file_name) if __name__ == "__main__": main()