n在每个
<br />元素的末尾添加一个字符应该可以得到预期的结果:
>>> import lxml.html as html>>> 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>'>>> doc = html.document_fromstring(fragment)>>> for br in doc.xpath("*//br"): br.tail = "n" + br.tail if br.tail else "n">>> doc.text_content()'This is a text node.nThis is another text node.nnAnd a child element.Another child,n with two text nodes'>>> 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>'
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)