漂亮的汤桌,别进去了

2024-09-25 00:29:34 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好,我有一些正在解析的html,这里是:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>

<body>
    <table class="dayinner">
        <tr class="lun">
            <td class="mealname" colspan="3">LUNCH</td>
        </tr>

        <tr class="lun">
            <td class="station">&nbsp;Deli</td>

            <td class="menuitem">
                <div class="menuitem">
                    <input class="chk" id="S1L0000010000047598_35356" onclick=
                    "rptlist(this);" onmouseout="wschk(0);" onmouseover=
                    "wschk(1);" type="checkbox" /> <span class="ul" onclick=
                    "nf('0000047598_35356');" onmouseout="pcls(this);"
                    onmouseover="ws(this);">Made to Order Deli Core</span>
                </div>
            </td>

            <td class="price"></td>
        </tr>

        <tr class="lun">
            <td class="station">&nbsp;</td>

            <td class="menuitem">
                <div class="menuitem">
                    <input class="chk" id="S1L0000020000046033_63436" onclick=
                    "rptlist(this);" onmouseout="wschk(0);" onmouseover=
                    "wschk(1);" type="checkbox" /> <span class="ul" onclick=
                    "nf('0000046033_63436');" onmouseout="pcls(this);"
                    onmouseover="ws(this);">Chicken Caesar Wrap</span>
                </div>
            </td>

            <td class="price"></td>
        </tr>

        <tr class="lun">
            <td colspan="3" style="height:3px;"></td>
        </tr>

        <tr class="lun">
            <td colspan="3" style="background-color:#c0c0c0; height:1px;"></td>
        </tr>

        <tr class="lun">
            <td class="station">&nbsp;Dessert</td>
            <td class="station">&nbsp;</td>

            <td class="menuitem">
                <div class="menuitem">
                    <input class="chk" id="S1L0000020000046033_63436" onclick=
                    "rptlist(this);" onmouseout="wschk(0);" onmouseover=
                    "wschk(1);" type="checkbox" /> <span class="ul" onclick=
                    "nf('0000046033_63436');" onmouseout="pcls(this);"
                    onmouseover="ws(this);">Chicken Caesar Wrap</span>
                </div>
            </td>
        </tr>
    </table>
</body>
</html>

这是我的代码,我只想要熟食区下的项目,通常我不知道有多少有这样的方法吗?在

^{pr2}$

但是这只在有两个项目的情况下有效,如果项目的数量未知,我怎么能让它工作呢?在

提前谢谢


Tags: divhtmlthistrclasstdspanstation
1条回答
网友
1楼 · 发布于 2024-09-25 00:29:34

您可以将find_all函数中的text属性用于1。查找其station列包含子字符串Deli的所有行。。2循环遍历每一行并找到classul的那一行中的跨距。在

import re
soup = BeautifulSoup(text)

tds_deli = soup.find_all(name='td', attrs={'class':'station'}, text=re.compile('Deli'))

for td in tds_deli:
    try:
        tr = td.find_parent()
        spans = tr.find_all('span', {'class':'ul'})
        for span in spans:
            # do something
            print span.text
        print '      one row       -'
    except:
        pass

本例中的示例输出:

^{pr2}$

不确定我是否正确地理解了这个问题,但我认为我的代码可以帮助你开始。在

相关问题 更多 >