[PYTHON] BeautifulSoup trick: ask for root tag

How to find the root [document] tag from any BeautifulSoup tag. It was a method I wish I had, but for some reason it wasn't there.

bs4_root.py


from bs4 import BeautifulSoup

def root(self):
    if self.name == u'[document]':
        return self
    else:
        return [node for node in self.parents][-1]

BeautifulSoup.root = root

The method find_parent (u'[document]') is fine, but it takes longer than the code above.

Recommended Posts

BeautifulSoup trick: ask for root tag
BeautifulSoup trick: Decide the Tag by specifying the path
[Python] xmp tag for photos