lxml.html.fromstring 坚持将所有内容包装在标签中(p
默认)。从这个标签树来看,
<p>this is <b>the</b> good stuff<p>
我想提取字符串:
this is <b>the</b> good stuff
我该怎么做呢?
这通常被称为“内部 xml”而不是“内部文本”。这是获取元素内部 xml 的一种可能方法:
import lxml.etree as etree
import lxml.html
html = "<p>this is <b>the</b> good stuff<p>"
tree = lxml.html.fromstring(html)
node = tree.xpath("//p")[0]
result = node.text + ''.join(etree.tostring(e) for e in node)
print(result)
output :
this is <b>the</b> good stuff
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)