Beautifulsoup,在htm中找到唯一没有属性的标签

时间:2020-04-01 00:04:57

标签: python beautifulsoup

我知道...从标题开始,这个答案似乎与其他数千个答案相同。但是我仍然搜索了所有相关和相似的问题。我要问的是,给定这个html(只是一个例子):

<html>

<body>
    <div class="div-share noprint">
        <div class="addthis_toolbox addthis_default_style">
            <a class="btn btn-xs btn-share addthis_button_facebook" href="https://somelink" target="_blank">
                <span class="playblk"><img alt="someimg" class="playblk" height="25" src="some source" title="sometitle" width="25"/></span>
            </a>
            <a class="btn btn-xs btn-share addthis_button_facebook" href="https://somelink" target="_blank">
                <span class="playblk"><img alt="someimg" class="playblk" height="25" src="some source" title="sometitle" width="25"/></span>
            </a>
        </div>
    </div>
    <div class="addthis_toolbox addthis_default_style">
        <a class="btn btn-xs btn-share addthis_button_facebook" href="https://somelink" target="_blank">
            <span class="playblk"><img alt="some img" class="playblk" height="25" src="othersource" title="some othertitle" width="25"/></span>
        </a>
    </div>
    <div class="div-share">
        <h1>"The Divine Wings Of Tragedy" lyrics</h1></div>,
    <div class="pther">
        <h2><b>Symphony X Lyrics</b></h2>
    </div>
    <div class="ringtone">
        <span id="cf_text_top"></span>
    </div>
    <div>
        <i>[Part I - At the Four Corners of the Earth]</i>
        <br/>
        <br/> On the edge of paradise
        <br/> Tears of woe fall, cold as ice
        <br/> Hear my cry
        <br/>
    </div>
</body>

</html>

我想找到唯一没有属性的标签。就像我在其他问题中看到的那样,不是Empy attr,也不是奇怪的特定属性,或者不是attrs = None ...该标签没有别的。但是,如果我使用findAll,则会在html中找到所有其他标记。如果我使用attrs = False,attrs = None,依此类推。

那么有可能吗?

非常感谢!

1 个答案:

答案 0 :(得分:0)

您可以将lambda函数传递给find_all方法,该方法检查标记名称,并且元素内没有attrs:

soup.find_all(lambda tag: tag.name == 'div' and not tag.attrs)
相关问题