TAGS :Viewed: 1 - Published at: a few seconds ago

[ Checking for particular attributes using BeatifulSoup ]

I'm trying to analyze a block of html and try to extract those tags that have a style attribute on them. For example,

<ul class="dropdown-menu" style="text-align:left; width:100%; color:#003; margin-left:-67px;">

Those should trigger my rule. But inline style tags should not get caught by my rule. Something like

<style>
.scroll-pane-arrows
    {
        width: 100%;
        height: 235px;
        overflow: auto;
    }
</style>

should not trigger my rule. Now, I'm thinking of a way to find all the style attributes and not the tags. I'd have to have a function that locates all the tags and then find the style attribute within them. But how do I run through all the tags alone? Something like findAll("tag_name") would get it for me, sure. But I can't do this for every tag. There are so many tags that could use the style attribute and it would be a lot of overhead to search through each of them using the findAll function. And I might miss out some tags. Is there any easier way of just finding all tags so that I can further search through for the style attribute?

Answer 1


From the documentation, the tag name is optional. You should be able to just use findAll(True) For more information: http://www.crummy.com/software/BeautifulSoup/bs3/documentation.html