Python 和 XML 添加到 Feed RSS

Python and XML Append for Feed RSS

我正在开发一个生成 Feed RSS (XML) 的简单 Python 脚本。

但我不能在旧提要之前添加新提要,以便第一个 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>

我试了几次,还是不明白哪里错了。

谁能帮帮我?

谢谢 安德里亚

您想将其添加到 description 节点之后,您可以使用 addnext:

channel.find(".//description").addnext(item)

print ET.tostring(channel, pretty_print=True, xml_declaration=True)

或第一个 item 之前,您可以使用 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]channel 节点,因此附加到它只是在末尾添加新节点。