【发布时间】:2014-08-31 05:57:19
【问题描述】:
我有这个代码:
from lxml.html import fromstring, tostring
html = "<p><img src='some_pic.jpg' />Here is some text</p>"
doc = fromstring(html)
img = doc.find('.//img')
doc.remove(img)
print tostring(doc)
输出为:<p></p>
为什么删除 img 标签也会删除它后面的文本?也就是说,为什么没有打印出结果:<p>Here is some text</p>
我怎样才能删除该标签而不删除文本?请注意,即使我在 img 上包含显式结束标记,我也会得到相同的结果,即:
html = "<p><img src='some_pic.jpg'></img>Here is some text</p>"
【问题讨论】:
标签: python html html-parsing lxml lxml.html