首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >美丽的汤桌,停止获取信息

美丽的汤桌,停止获取信息
EN

Stack Overflow用户
提问于 2014-05-13 08:41:27
回答 1查看 213关注 0票数 0

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

代码语言:javascript
运行
复制
<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>

这是我的代码,我只想要熟食区下面的商品,通常我不知道有多少个商品,有没有办法做到这一点?

代码语言:javascript
运行
复制
soup = BeautifulSoup(open("upperMenu.html"))

title = soup.find('td', class_='station').text.strip()

spans = soup.find_all('span', class_='ul')[:2]

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

提前感谢

EN

回答 1

Stack Overflow用户

发布于 2014-05-13 11:34:47

您可以在find_all函数中使用text属性来1.查找其station列包含子字符串Deli的所有行。2.遍历每一行,找到该行中classul的跨度。

代码语言:javascript
运行
复制
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

本例中的输出示例:

代码语言:javascript
运行
复制
Made to Order Deli Core
------------one row -------------

我不确定我是否正确理解了这个问题,但我认为我的代码可能会帮助您入门。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23621107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档