【问题标题】:Parse Classes with similar names using Beautiful Soup in python在 python 中使用 Beautiful Soup 解析具有相似名称的类
【发布时间】:2017-10-31 18:49:16
【问题描述】:

我有一个 html 页面,我想从中提取具有类属性 boldtd 元素。而是弹出其他 td 元素,例如 dark bold

当我在 BeautifulSoup 中使用 findAll 方法时,

scores= soup.findAll(lambda tag: tag.name == 'td', { "class" : "bold"})

我得到了所有这些元素

 <td class="dark bold">
    <span class="hide-for-tablet">Sebastian</span>
    <span class="hide-for-mobile">Vettel</span>
    <span class="uppercase hide-for-desktop">VET</span>
    </td>
    <td class="bold hide-for-mobile">78</td>
    <td class="dark bold">1:44:44.340</td>
    <td class="bold">25</td>

我真正想要的是

&lt;td class="bold"&gt;25&lt;/td&gt;

如何缩小结果范围?

【问题讨论】:

    标签: html python-2.7 class beautifulsoup findall


    【解决方案1】:

    试试这个

    scores= soup.findAll(lambda tag: tag.name == 'td' and tag.get('class') == ['bold'])
    

    【讨论】:

    • @HideoDaikoku 谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 2013-10-21
    相关资源
    最近更新 更多