def flatten(nested):
    """recusively flatten nested elements
    yields individual elements
    """
for thing in nested:
    yield thing
for other in flatten(thing):
    yield other
doc = html.fromstring(s)
for thing in flatten(doc):
    pass
Синтаксический анализ Python XML без рута