I have some lxml element:
>> lxml_element.text
'hello BREAK world'
I need to replace the word BREAK with an HTML break tag—<br />. I’ve tried to do simple text replacing:
lxml_element.text.replace('BREAK', '<br />')
but it inserts the tag with escaped symbols, like <br/>. How do I solve this problem?
Here’s how you could do it. Setting up a sample lxml from your question:
Next, create a subelement tag <br>:
But that’s not all you want. You have to take the text before the <br> and place it in
.text:Then set the
.tailof the child to contain the rest of the text: