1

For some reason I am unable to extract the table from this simple html table.

from bs4 import BeautifulSoup
import requests

def main():
    html_doc = requests.get(
    'http://www.wolfson.cam.ac.uk/old-site/cgi/catering-menu?week=0;style=/0,vertical')

    soup = BeautifulSoup(html_doc.text, 'html.parser')
    table = soup.find('table')
    print table


if __name__ == '__main__':
    main()

I have the table, but I cannot understand the beautifulsoup documentation well enough to know how to extract the data. The data are in tr tags.

The website shows a simple HTML food menu.

I would like to output the day of the week and the menu for that day:

Monday: 
    Lunch: some_lunch, Supper: some_food
Tuesday:
    Lunch: some_lunch, Supper: some_supper

and so on for all the days of the week. 'Formal Hall' can be ignored.

How can I iterate over the tr tags so that I can create this output?

1
  • I've just checked the HTML source, and I can only see lot's of </td> </td> </tr> </td> </td> </td> </tr> </td> </td> </tr> </td> </td>...who wrote it? Commented Nov 20, 2015 at 4:28

1 Answer 1

1

I normally don't provide direct solutions. You should've tried some code and if you face any issue then post it here. But anyways, this is what I've written and it should help in giving you a head start.

soup = BeautifulSoup(r.content)

rows = soup.findAll("tr")

for i in xrange(1,8):
    row = rows[i]
    print row.find("th").text
    for j in xrange(0,2):
        print rows[0].findAll("th")[j+1].text.strip(), ": ",
        td = row.findAll("td")[j]
        for p in td.findAll("p"):
            print p.text, ",",
        print
    print

Output will look something like this:

Monday
Lunch:  Leek and Potato Soup, Spaghetti Bolognese with Garlic Bread, Red Pepper and Chickpea Stroganoff with Brown Rice, Chicken Goujons with Garlic Mayonnaise Dip, Vegetable Grills with Sweet Chilli Sauce, Coffee and Walnut Sponge with Custard,
Supper:  Leek and Potato Soup, Breaded Haddock with Lemon and Tartare Sauce, Vegetable Samosa with Lentil Dahl, Chilli Beef Wraps, Steamed Strawberry Sponge with Custard,

Tuesday
Lunch:  Tomato and Basil Soup, Pan-fried Harrisa Spiced Chicken with Roasted Vegetables, Vegetarian Spaghetti Bolognese with Garlic Bread, Jacket Potato with Various Fillings, Apple and Plum Pie with Custard,
Supper:  Tomato and Basil Soup, Lamb Tagine with Fruit Couscous, Vegetable Biryani with Naan Bread, Pan-fried Turkey Escalope, Raspberry Shortbread,
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.