user12941222
user12941222

Reputation:

Beautifulsoup, find the only tag in the htm that has no attribute

I know...from the title this answer seems the same oh thousand of others. BUT I have still searched all related and similar questions. What I'm asking is, given this html (just an exemple):

<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>

I want to find the only tag that has no attributes. Not an empy attr, like I saw in other questions, or a strange specific attribute, or attrs = None ... that tag has nothing else. But if I use findAll, I find all the other tag in the html. the same if I use attrs = False, attrs = None and so on..,

so there is a possibility?

thanks a lot!

Upvotes: 2

Views: 908

Answers (1)

drec4s
drec4s

Reputation: 8077

You can pass a lambda function to the find_all method that checks the tag name and that there are no attrs within the element:

soup.find_all(lambda tag: tag.name == 'div' and not tag.attrs)

Upvotes: 2

Related Questions