python可以获取某元素之前所有源码吗?

python可以获取某元素之前所有源码吗?
怎么实现?


story


aa
bb


cc

    ee

ff




story


p1p1p1
b1b1b1


aa



    bb

cc

a2a2a2
b2b2b2

p3p3p3



a3a3a3

p4p4p4




'''

from lxml import html

tree = html.fromstring(doc)
a = tree.get_element_by_id("a1")
print(html.tostring(a))
print(html.tostring(tree).decode())

def dropnode(e=None):
if e is None: return
if e.tag == 'body': return
nd = e.getnext()
while nd is not None:
nd.drop_tree()
nd = e.getnext()
dropnode(e.getparent())

dropnode(a)
print(html.tostring(tree).decode())