【发布时间】:2022-11-14 17:48:25
【问题描述】:
有没有办法使用python在XML中元素的开始和结束处插入文本
或使用 python 插入文本的任何配置文件
例如: 输入:
<html>
<head>
<title>Example page</title>
</head>
<body>
<p>Moved to <a href="http://example.org/">example.org</a>
or <a href="http://example.com/">example.com</a>.</p>
</body>
</html>
输出:
<html>
<head>
<title>Example page</title>
</head>
<body>
<p>Moved to <a href="http://example.org/">example.org</a>
or linktag{<a href="http://example.com/">example.com</a>}.</p>
</body>
</html>
import xml.etree.ElementTree as ET
tree = ET.parse('index.html')
root = tree.getroot()
for val in root.findall("./book/[price='5.95']"):
print(val.attrib)
for elem in root.findall("body/p/a"):
elem.tag = "linktag{"
tree.write("output.xhtml")
尝试但没有得到预期的输出
【问题讨论】:
标签: python-3.x