Python lxml包装元素

Chr*_*ris 6 python django lxml

我想知道使用lxml和Python用另一个元素包装元素的最简单方法是什么,例如,如果我有一个html片段:

<h1>The cool title</h1>
<p>Something Neat</p>
<table>
<tr>
<td>aaa</td>
<td>bbb</td>
</tr>
</table>
<p>The end of the snippet</p>
Run Code Online (Sandbox Code Playgroud)

我想用这样的section元素包装table元素:

<h1>The cool title</h1>
<p>Something Neat</p>
<section>
<table>
<tr>
<td>aaa</td>
<td>bbb</td>
</tr>
</table>
</section>
<p>The end of the snippet</p>
Run Code Online (Sandbox Code Playgroud)

我想做的另一件事是在xml文档中搜索具有特定属性的h1s,然后将所有元素包装到元素中的下一个h1标记中,例如:

<h1 class='neat'>Subject 1</h1>
<p>Here is a bunch of boring text</p>
<h2>Minor Heading</h2>
<p>Here is some more</p>
<h1 class='neat>Subject 2</h1>
<p>And Even More</p>
Run Code Online (Sandbox Code Playgroud)

转换成:

<section>
<h1 class='neat'>Subject 1</h1>
<p>Here is a bunch of boring text</p>
<h2>Minor Heading</h2>
<p>Here is some more</p>
</section>
<section>
<h1 class='neat>Subject 2</h1>
<p>And Even More</p>
</section>
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助,克里斯

JB.*_*JB. 5

lxml非常适合解析格式良好的xml,但如果你有非xhtml html则不太好.如果是这种情况,那么请按照系统化程序的建议选择BeautifulSoup.

使用lxml,这是在文档中的所有表周围插入一个部分的相当简单的方法:

import lxml.etree

TEST="<html><h1>...</html>"

def insert_section(root):
    tables = root.findall(".//table")
    for table in tables:
        section = ET.Element("section")
        table.addprevious(section)
        section.insert(0, table)   # this moves the table

root = ET.fromstring(TEST)
insert_section(root)
print ET.tostring(root)
Run Code Online (Sandbox Code Playgroud)

您可以执行类似的操作来包装标题,但是您需要遍历要包装的所有元素并将它们移动到该部分.element.index(子)和列表切片可能在这里有所帮助.