我正在使用lxml解析一些HTML片段(来自RSS提要),为了高效地完成这项工作,我使用了create_parent='div'
。当我稍后输出HTML时,我不希望包含父div,因为在我的html布局中,它最终成为div中的一个div,这是完全不安全的。
现在的代码是:
from lxml.html import fragment_fromstring
html = fragment_fromstring(html_string, create_parent = 'div')
for tag in html.xpath('//*[@class]'):
tag.attrib.pop('class')
for tag in html.xpath('//*[@id]'):
tag.attrib.pop('id')
return lxml.html.tostring(html)
TL;DR:如何在输出时删除包装div?
发布于 2013-06-29 15:08:01
提取子元素。
return '\n'.join(lxml.html.tostring(x) for x in html.iterchildren())
https://stackoverflow.com/questions/17381273
复制相似问题