[Tutor] Trim away parts of beautiful soup

Julius Hamilton juliushamilton100 at gmail.com
Sat Oct 16 17:40:44 EDT 2021


Hey,

I am exploring navigating a Beautiful Soup tree element by element starting
from the top. I look at what the element contains with the .attrs method.
If the tag is not needed, I delete it with .unwrap(). If the contents in an
element are not needed, I delete it with .decompose(). I learn what the
next round of elements is with:

for child in element.children:
  child.name.

I would prefer an easier way to do this than writing out the name of every
element, like soup.body.div.main.

Is there more of an application way to navigate the tree?

You could start on the “top node” and just call simple methods like “next
node” or “child node” and “name” and “contents” and “delete”.

Thanks very much,
Julius


More information about the Tutor mailing list