I'm iterating through an HTML tree with lxml iterwalk and I'd like to replace all <br>
tags inside <pre></pre>
with new line characters. That's what I have so far:
root = lxml.html.fromstring(text)
for action, el in etree.iterwalk(root):
if el.tag == 'pre':
for br in el.xpath('br'):
# replace this <br> tag with "\n"
If possible in any way, the replacement should really be done inside this loop, because we need the loop anyways and including this step in there would probably be the most efficient way.
There's a similar question/answer on SO, but it didn't help solve the problem: How can one replace an element with text in lxml?