Python 如何从简单html表中提取行?

Python 如何从简单html表中提取行?,python,html,beautifulsoup,Python,Html,Beautifulsoup,由于某种原因,我无法从这个简单的html表中提取该表 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.

由于某种原因,我无法从这个简单的html表中提取该表

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()
我有这个表,但我不能很好地理解beautifulsoup文档,因此无法了解如何提取数据。数据位于
tr
标签中

该网站显示了一个简单的HTML食品菜单

我想输出一周中的某一天以及该天的菜单:

Monday: 
    Lunch: some_lunch, Supper: some_food
Tuesday:
    Lunch: some_lunch, Supper: some_supper
一周中的所有日子都是如此。”“正式大厅”可以忽略


如何迭代
tr
标记以创建此输出?

我通常不提供直接的解决方案。你应该试过一些代码,如果你遇到任何问题,就把它贴在这里。但无论如何,这是我写的,它应该有助于给你一个领先的开始

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 汤=美汤(r.含量) 行=soup.findAll(“tr”) 对于x范围内的i(1,8): 行=行[i] 打印行.查找(“th”).文本 对于X范围内的j(0,2): 打印行[0]。findAll(“th”)[j+1]。text.strip(),“:”, td=第行findAll(“td”)[j] 对于td.findAll中的p(“p”): 打印p.text,“,”, 打印 打印 输出将如下所示:

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, 星期一 午餐:韭菜土豆汤,意大利肉酱面配大蒜面包,红辣椒和鹰嘴豆配糙米,鸡肉Goujons配大蒜蛋黄酱蘸酱,蔬菜烤架配甜辣椒酱,咖啡和核桃海绵配奶油冻, 晚餐:韭菜和土豆汤,柠檬和鞑靼沙司黑线鳕面包,扁豆达尔蔬菜沙摩沙,辣椒牛肉卷,奶油冻蒸草莓海绵, 星期二 午餐:番茄和罗勒汤、烤蔬菜油炸哈里萨五香鸡、大蒜面包素食意大利肉酱面、各种馅料的茄克土豆、苹果和洋李馅饼、奶油冻、, 晚餐:西红柿罗勒汤,羊肉塔吉恩配水果蒸粗麦粉,蔬菜比里亚尼配纳安面包,油炸火鸡烤饼,覆盆子短面包,
我刚刚检查了HTML源代码,我只能看到很多
…谁写的?