Issue
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?
Solution
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
print common_parent.name
It'll basically walk both elements' parents from root down, and return the last common one.
Answered By - Joachim Isaksson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.