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