python生成RSS(PyRSS2Gen)

时间:2023-03-08 19:57:40
python生成RSS(PyRSS2Gen)

既然能够用python解析rss,那么也顺带研究下生成rss。

其实很简单,只是生成一个比较特殊点的xml文档而已。

这里我使用了PyRss2Gen,用法很简单,看代码就知道了,如下:

 import datetime
import PyRSS2Gen rss = PyRSS2Gen.RSS2(
title = "Andrew's PyRSS2Gen feed",
link = "http://www.dalkescientific.com/Python/PyRSS2Gen.html",
description = "The latest news about PyRSS2Gen, a "
"Python library for generating RSS2 feeds", lastBuildDate = datetime.datetime.now(), items = [
PyRSS2Gen.RSSItem(
title = "PyRSS2Gen-0.0 released",
link = "http://www.dalkescientific.com/news/030906-PyRSS2Gen.html",
description = "Dalke Scientific today announced PyRSS2Gen-0.0, "
"a library for generating RSS feeds for Python. ",
guid = PyRSS2Gen.Guid("http://www.dalkescientific.com/news/"
"030906-PyRSS2Gen.html"),
pubDate = datetime.datetime(2003, 9, 6, 21, 31)),
PyRSS2Gen.RSSItem(
title = "Thoughts on RSS feeds for bioinformatics",
link = "http://www.dalkescientific.com/writings/diary/"
"archive/2003/09/06/RSS.html",
description = "One of the reasons I wrote PyRSS2Gen was to "
"experiment with RSS for data collection in "
"bioinformatics. Last year I came across...",
guid = PyRSS2Gen.Guid("http://www.dalkescientific.com/writings/"
"diary/archive/2003/09/06/RSS.html"),
pubDate = datetime.datetime(2003, 9, 6, 21, 49)),
]) rss.write_xml(open("pyrss2gen.xml", "w"))

这个生成的rss文档是这样的:

 <?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
<channel>
<title>Andrew's PyRSS2Gen feed</title>
<link>http://www.dalkescientific.com/Python/PyRSS2Gen.html</link>
<description>The latest news about PyRSS2Gen, a Python library for generating RSS2 feeds</description>
<lastBuildDate>Thu, 15 May 2014 20:24:12 GMT</lastBuildDate>
<generator>PyRSS2Gen-1.1.0</generator>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<item>
<title>PyRSS2Gen-0.0 released</title>
<link>http://www.dalkescientific.com/news/030906-PyRSS2Gen.html</link>
<description>Dalke Scientific today announced PyRSS2Gen-0.0, a library for generating RSS feeds for Python. </description>
<guid isPermaLink="true">http://www.dalkescientific.com/news/030906-PyRSS2Gen.html</guid>
<pubDate>Sat, 06 Sep 2003 21:31:00 GMT</pubDate>
</item>
<item>
<title>Thoughts on RSS feeds for bioinformatics</title>
<link>http://www.dalkescientific.com/writings/diary/archive/2003/09/06/RSS.html</link>
<description>One of the reasons I wrote PyRSS2Gen was to experiment with RSS for data collection in bioinformatics. Last year I came across...</description>
<guid isPermaLink="true">http://www.dalkescientific.com/writings/diary/archive/2003/09/06/RSS.html</guid>
<pubDate>Sat, 06 Sep 2003 21:49:00 GMT</pubDate>
</item>
</channel>
</rss>