【问题标题】:Beautifulsoup, find the only tag in the htm that has no attributeBeautifulsoup,找到htm中唯一没有属性的标签
【发布时间】:2020-07-12 15:54:47
【问题描述】:

我知道...从标题来看,这个答案似乎与其他人一样。但我仍然搜索了所有相关和类似的问题。我要问的是,鉴于这个 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 等等,也是一样的......,

那么有可能吗?

非常感谢!

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    您可以将一个 lambda 函数传递给 find_all 方法,该方法检查标签名称以及元素中没有属性:

    soup.find_all(<b>lambda tag: tag.name == 'div' and not tag.attrs</b>)

    【讨论】:

      猜你喜欢
      • 2018-08-18
      • 1970-01-01
      • 2021-02-11
      • 1970-01-01
      • 2018-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      相关资源
      最近更新 更多