用于RSS提要的Python和XML附加
我正在开发一个简单的Python脚本,用于生成提要RSS(XML) 但是我不能在旧的提要之前添加新的提要,这样第一个rss总是最新的新闻 这是我用来生成初始XML文件的代码:用于RSS提要的Python和XML附加,python,xml,rss,Python,Xml,Rss,我正在开发一个简单的Python脚本,用于生成提要RSS(XML) 但是我不能在旧的提要之前添加新的提要,这样第一个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.SubE
#!/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 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>
W3学校主页
http://www.w3schools.com
免费网页建设教程
RSS教程
http://www.w3schools.com/xml/xml_rss.asp
新的学校RSS教程
**
第二次插入
http://second.test
说明2
**
但我想得到:
<?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>
W3学校主页
http://www.w3schools.com
免费网页建设教程
**
第二次插入
http://second.test
说明2
**
RSS教程
http://www.w3schools.com/xml/xml_rss.asp
新的学校RSS教程
我做了几次尝试,但不明白我错在哪里
谁能帮我
多谢各位
Andrea您想将其添加到描述节点之后,您可以使用该节点: 或在您可以使用的第一项之前: 两者都将为您提供:
<?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>
W3学校主页
http://www.w3schools.com
免费网页建设教程
第二次插入
http://second.test
说明2
RSS教程
http://www.w3schools.com/xml/xml_rss.asp
新的学校RSS教程
channel[0]
是通道节点,因此附加到该节点只会在末尾添加新节点 要将其添加到“描述”节点之后,可以使用:
或在您可以使用的第一项之前:
两者都将为您提供:
<?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>
W3学校主页
http://www.w3schools.com
免费网页建设教程
第二次插入
http://second.test
说明2
RSS教程
http://www.w3schools.com/xml/xml_rss.asp
新的学校RSS教程
channel[0]
是通道节点,因此附加到该节点只会在末尾添加新节点
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>