既然能够用python解析rss,那么也顺带研究下生成rss。
其实很简单,只是生成一个比较特殊点的xml文档而已。
这里我使用了PyRss2Gen,用法很简单,看代码就知道了,如下:
1 import datetime 2 import PyRSS2Gen 3 4 rss = PyRSS2Gen.RSS2( 5 title = "Andrew‘s PyRSS2Gen feed", 6 link = "http://www.dalkescientific.com/Python/PyRSS2Gen.html", 7 description = "The latest news about PyRSS2Gen, a " 8 "Python library for generating RSS2 feeds", 9 10 lastBuildDate = datetime.datetime.now(), 11 12 items = [ 13 PyRSS2Gen.RSSItem( 14 title = "PyRSS2Gen-0.0 released", 15 link = "http://www.dalkescientific.com/news/030906-PyRSS2Gen.html", 16 description = "Dalke Scientific today announced PyRSS2Gen-0.0, " 17 "a library for generating RSS feeds for Python. ", 18 guid = PyRSS2Gen.Guid("http://www.dalkescientific.com/news/" 19 "030906-PyRSS2Gen.html"), 20 pubDate = datetime.datetime(2003, 9, 6, 21, 31)), 21 PyRSS2Gen.RSSItem( 22 title = "Thoughts on RSS feeds for bioinformatics", 23 link = "http://www.dalkescientific.com/writings/diary/" 24 "archive/2003/09/06/RSS.html", 25 description = "One of the reasons I wrote PyRSS2Gen was to " 26 "experiment with RSS for data collection in " 27 "bioinformatics. Last year I came across...", 28 guid = PyRSS2Gen.Guid("http://www.dalkescientific.com/writings/" 29 "diary/archive/2003/09/06/RSS.html"), 30 pubDate = datetime.datetime(2003, 9, 6, 21, 49)), 31 ]) 32 33 rss.write_xml(open("pyrss2gen.xml", "w"))
这个生成的rss文档是这样的:
1 <?xml version="1.0" encoding="iso-8859-1"?> 2 <rss version="2.0"> 3 <channel> 4 <title>Andrew‘s PyRSS2Gen feed</title> 5 <link>http://www.dalkescientific.com/Python/PyRSS2Gen.html</link> 6 <description>The latest news about PyRSS2Gen, a Python library for generating RSS2 feeds</description> 7 <lastBuildDate>Thu, 15 May 2014 20:24:12 GMT</lastBuildDate> 8 <generator>PyRSS2Gen-1.1.0</generator> 9 <docs>http://blogs.law.harvard.edu/tech/rss</docs> 10 <item> 11 <title>PyRSS2Gen-0.0 released</title> 12 <link>http://www.dalkescientific.com/news/030906-PyRSS2Gen.html</link> 13 <description>Dalke Scientific today announced PyRSS2Gen-0.0, a library for generating RSS feeds for Python. </description> 14 <guid isPermaLink="true">http://www.dalkescientific.com/news/030906-PyRSS2Gen.html</guid> 15 <pubDate>Sat, 06 Sep 2003 21:31:00 GMT</pubDate> 16 </item> 17 <item> 18 <title>Thoughts on RSS feeds for bioinformatics</title> 19 <link>http://www.dalkescientific.com/writings/diary/archive/2003/09/06/RSS.html</link> 20 <description>One of the reasons I wrote PyRSS2Gen was to experiment with RSS for data collection in bioinformatics. Last year I came across...</description> 21 <guid isPermaLink="true">http://www.dalkescientific.com/writings/diary/archive/2003/09/06/RSS.html</guid> 22 <pubDate>Sat, 06 Sep 2003 21:49:00 GMT</pubDate> 23 </item> 24 </channel> 25 </rss>