'Conditions in loop to ensure python only scrapes single div

While attempting to scrape this website: https://dining.umich.edu/menus-locations/dining-halls/mosher-jordan/ I have located the food item names by doing the following:

import requests
from bs4 import BeautifulSoup

url = "https://dining.umich.edu/menus-locations/dining-halls/mosher-jordan/"
req = requests.get(url, headers)
soup = BeautifulSoup(req.content, 'html.parser')

foodLocation = soup.find_all('div', class_='item-name')

for singleFood in foodLocation:
    food = singleFood.text
    print(food)

The problem is, I only want to print the food inside of the "World Palate Maize" section seen in the Lunch portion of the link. In the HTML, there are multiple divs that all contain the foods within a certain type (World Palate Maize, Hot Cereal, MBakery etc.) I'm having trouble figuring out how to tell the loop to only print inside of a certain section (certain div?). This may require an if statement or condition in the for loop but I am unsure about how to format/what to use as a condition to ensure this loop only prints the content from one section.



Solution 1:[1]

Seems like "Lunch" would always be the second div, so you can probably do

import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'Mozilla'
}

url = "https://dining.umich.edu/menus-locations/dining-halls/mosher-jordan/"
req = requests.get(url, headers)
soup = BeautifulSoup(req.content, 'html.parser')

[breakfast, lunch, dinner] = soup.select('div#mdining-items div.courses')
foods = lunch.select('div.item-name')

for food in foods:
    print(food.text)

Solution 2:[2]

One strategy could be to select more specific by text e.g. with css selectors:

soup.select('h3:-soup-contains("Lunch")+div h4:-soup-contains("World Palate Maize") + ul .item-name')

Example

import requests
from bs4 import BeautifulSoup

url = "https://dining.umich.edu/menus-locations/dining-halls/mosher-jordan/"
req = requests.get(url)
soup = BeautifulSoup(req.content, 'html.parser')

foodLocation = soup.select('h3:-soup-contains("Lunch")+div h4:-soup-contains("World Palate Maize") + ul .item-name')

for singleFood in foodLocation:
    food = singleFood.text
    print(food)

Output

Mojo Grilled Chicken
Italian White Bean Salad

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 xxMrPHDxx
Solution 2 HedgeHog