Tôi muốn giữ lại các thẻ <br>
làm \n
khi trích xuất nội dung văn bản từ các phần tử lxml.Tôi làm cách nào để bảo vệ <br> làm dòng mới với lxml.html text_content() hoặc tương đương?
Ví dụ mã:
fragment = '<div>This is a text node.<br/>This is another text node.<br/><br/><span>And a child element.</span><span>Another child,<br> with two text nodes</span></div>'
h = lxml.html.fromstring(fragment)
Output:
> h.text_content()
'This is a text node.This is another text node.And a child element.Another child, with two text nodes'
Trông giống như thế nào sau khi phân tích cú pháp? –