Ionut Hulub Ionut Hulub - 1 year ago 338
Python Question

BeautifulSoup lowest common ancestor

Does the BeautifulSoup library for Python have any function that can take a list of nodes and return the lowest common ancestor?

If not, has any of you ever implemented such a function and care to share it?

Answer Source

I think this is what you want, with link1 being one element and link2 being another;

link_1_parents = list(link1.parents)[::-1]
link_2_parents = list(link2.parents)[::-1]

common_parent = [x for x,y in zip(link_1_parents, link_2_parents) if x is y][-1]

print common_parent

It'll basically walk both elements' parents from root down, and return the last common one.

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