我想将两个段落中的所有内容合并为一个段落,并在它们之间留一个空格。我怎么能用 lxml 做到这一点?
例子:
<p>He is <b>bold</b>!</p>
<p>Is he <u>here</u>?</p>
将合并为:
<p>He is <b>bold</b>! Is he <u>here</u>?</p>
最佳答案
如果您的结构很简单,这可能会奏效:
import lxml
from lxml import etree
root = etree.fromstring("<root></root>")
first = etree.fromstring("<p>He is <b>bold</b>!</p>")
second = etree.fromstring("<p>Is he <u>here</u>?</p>")
try:
first.getchildren()[-1].tail += ' ' + second.text
except IndexError:
first.text += ' ' + second.text
root.append(first)
for child in second.getchildren():
root.append(child)
etree.tostring(root)
关于python - 用 lxml 合并两个段落的 HTML 内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44562565/