George George - 1 year ago 138
Python Question

BeautifulSoup, what order does .descendants go in?

When using the beautifulsoup soup.descendants iterator, are the tags yielded guaranteed to come back in any particular order? Since the documentation mentions that things are iterated over recursively, I'd guess that things are returned in DFS order, but I'm not sure if this is something I can rely on.

Answer Source

The documentation does not promise anything, but the source reveals all. .descendants yields tags in .next_element order:

The .next_element attribute of a string or tag points to whatever was parsed immediately afterwards. It might be the same as .next_sibling, but it’s usually drastically different.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download