BeautifulSoup - How to remove nested tags with no text and blank space from the end of HTML
Question: I'm trying to use BeautifulSoup to remove tags that have no text inside of
them. For example I have the following tags:

    
    
    <p>
       <p>
           <br/>
       </p>
    </p>
    

or

    
    
    <p>
       <br/>
    </p>
    

I have the following function:

    
    
    @staticmethod
    def stripTagWithNoText(soup,tagname,**kwargs):
        """Strip tags with no text"""
        #Make sure that soup and tags were defined
        assert isinstance(tagname,str)
    
        #Remove tags with no text
        for tag in soup.find_all(tagname):
            if tag.string:
                continue
            for subtag in tag.findChildren():
                if subtag.string:
                    break
            else:
                continue
            tag.extract()
    

However this is also removing tags such as the following:

    
    
    <p>This is some random text</p>
    

Can anyone spot what is wrong with this?

Additionally, suppose I have the following appended to the end of my html:

    
    
    <p><br />
    </p><p><br /> 
    </p><p><br />
    </p><p><br /> 
    </p><p><br />
    </p><p><br />
    </p>
    

Is there someway to remove all blank spaces from the end of the html similar
to string_text.strip()?

**Note** I am using Python3, bs4


Answer: Does this work for you?

    
    
    from bs4 import BeautifulSoup
    from bs4.element import Tag
    
    def main():
        test = """
        <p>
        this should not be here
           <p>this should not be here
               <br/>this should not be here
           </p>
           this should not be here
        </p>
        """
        soup = BeautifulSoup(test, 'html.parser')
    
        def stripTagWithNoText(soup, tagname):
            def remove(node):
                for index, item in enumerate(node.contents):
                    if isinstance(item, Tag):
                        remove(node.contents[index])
                    else:
                        node.contents[index] = ''
    
            #Remove tags with no text
            for tag in soup.find_all(tagname):
                remove(tag)
            print(soup)
    
        stripTagWithNoText(soup, 'p')
        return 0
    
    if __name__ == '__main__':
        main()
    

