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.