我正在开发一个生成Feed RSS(XML)的简单Python脚本。
但我不能在旧版之前添加新的Feed,以便让第一个rss始终是最新消息。
这是我用来生成初始XML文件的代码:
#!/usr/bin/env python
from lxml import etree as ET
root = ET.Element("rss")
root.set("version", "2.0")
channel = ET.SubElement(root, "channel")
title = ET.SubElement(channel, "title")
title.text = "W3Schools Home Page"
link = ET.SubElement(channel, "link")
link.text = "http://www.w3schools.com"
description = ET.SubElement(channel, "description")
description.text = "Free web building tutorials"
item = ET.SubElement(channel, "item")
title = ET.SubElement(item, "title")
title.text = "RSS Tutorial"
link = ET.SubElement(item, "link")
link.text = "http://www.w3schools.com/xml/xml_rss.asp"
description = ET.SubElement(item, "description")
description.text = "New RSS tutorial on W3Schools"
print ET.tostring(root, pretty_print=True, xml_declaration=True)
#write to file:
tree = ET.ElementTree(root)
tree.write('feed.xml', pretty_print=True, xml_declaration=True)
这是输出:
<?xml version='1.0' encoding='ASCII'?>
<rss version="2.0">
<channel>
<title>W3Schools Home Page</title>
<link>http://www.w3schools.com</link>
<description>Free web building tutorials</description>
<item>
<title>RSS Tutorial</title>
<link>http://www.w3schools.com/xml/xml_rss.asp</link>
<description>New RSS tutorial on W3Schools</description>
</item>
</channel>
</rss>
但是,如果我可以尝试使用此代码添加新的XML值:
#!/usr/bin/env python
from lxml import etree as ET
parser = ET.XMLParser(remove_blank_text=True)
tree = ET.parse("feed.xml", parser)
channel = tree.getroot()
item = ET.SubElement(channel, "item")
title = ET.SubElement(item, "title")
title.text = "Second Insert"
link = ET.SubElement(item, "link")
link.text = "http://second.test"
description = ET.SubElement(item, "description")
description.text = "description2"
channel[0].append(item)
print ET.tostring(channel, pretty_print=True, xml_declaration=True)
tree = ET.ElementTree(channel)
tree.write("feed.xml", pretty_print=True, xml_declaration=True)
结果如下:
<?xml version='1.0' encoding='ASCII'?>
<rss version="2.0">
<channel>
<title>W3Schools Home Page</title>
<link>http://www.w3schools.com</link>
<description>Free web building tutorials</description>
<item>
<title>RSS Tutorial</title>
<link>http://www.w3schools.com/xml/xml_rss.asp</link>
<description>New RSS tutorial on W3Schools</description>
</item>
**<item>
<title>Second Insert</title>
<link>http://second.test</link>
<description>description2</description>
</item>**
</channel>
</rss>
但我想得到:
<?xml version='1.0' encoding='ASCII'?>
<rss version="2.0">
<channel>
<title>W3Schools Home Page</title>
<link>http://www.w3schools.com</link>
<description>Free web building tutorials</description>
**<item>
<title>Second Insert</title>
<link>http://second.test</link>
<description>description2</description>
</item>**
<item>
<title>RSS Tutorial</title>
<link>http://www.w3schools.com/xml/xml_rss.asp</link>
<description>New RSS tutorial on W3Schools</description>
</item>
</channel>
</rss>
我做了几次尝试,但不明白我错在哪里。
谁能帮帮我?
谢谢 安德烈
答案 0 :(得分:0)
您希望在 description 节点之后添加它,您可以使用addnext执行此操作:
channel.find(".//description").addnext(item)
print ET.tostring(channel, pretty_print=True, xml_declaration=True)
或者您可以使用addprevious执行的第一个项目之前:
channel.find(".//item").addprevious(item)
两者都会给你:
<?xml version='1.0' encoding='ASCII'?>
<rss version="2.0">
<channel>
<title>W3Schools Home Page</title>
<link>http://www.w3schools.com</link>
<description>Free web building tutorials</description>
<item>
<title>Second Insert</title>
<link>http://second.test</link>
<description>description2</description>
</item>
<item>
<title>RSS Tutorial</title>
<link>http://www.w3schools.com/xml/xml_rss.asp</link>
<description>New RSS tutorial on W3Schools</description>
</item>
</channel>
</rss>
channel[0]
是频道节点,因此附加到该节点只是在最后添加新节点。