rss feed generation

Tim Golden mail at
Fri Sep 12 09:59:54 CEST 2008

Walter Cruz wrote:
> On Tue, Sep 9, 2008 at 9:35 PM, alex23 <wuwei23 at> wrote:
>> On Sep 10, 7:30 am, "Blake Garner" <trodemas... at> wrote:
>>> I'm looking for suggestions on how to approach generating rss feed
>>> .xml files using python. What modules to people recommend I start
>>> with?

I just rolled something with ElementTree. No idea how it
stacks up against validity checks, but it seems to be
accepted by a couple of standard feed readers.

(sorry; longish code segment)

import os, sys
from datetime import datetime
  from xml.etree import cElementTree as ET
except ImportError:
  from elementtree import ElementTree as ET

class Feed (object):
  ENCODING = "utf-8"
  def __init__ (self, title, link, description=None, author=None, updated_on=None):
    self.title = title = link
    self.description = description = author
    self.updated_on = updated_on or ()
    self.entries = []
  def add_entry (self, title, link, content, summary=None, id=None, updated_on=None):
    self.entries.append (
      dict (
        id=id or link, 
        updated_on=updated_on or ()

  def write_rss (self, ofile=sys.stdout, encoding=ENCODING):
    rss = ET.Element ("rss", version="2.0")
    channel = ET.SubElement (rss, "channel")
    ET.SubElement (channel, "title").text = self.title
    ET.SubElement (channel, "link").text =
    ET.SubElement (channel, "description").text = self.description
    for entry in self.entries:
      item = ET.SubElement (channel, "item")
      ET.SubElement (item, "title").text = entry['title']
      ET.SubElement (item, "link").text = entry['link']
      ET.SubElement (item, "guid").text = entry['id']
      ET.SubElement (item, "description").text = entry['content']
    ET.ElementTree (rss).write (ofile, encoding=encoding)
  def write_atom (self, ofile=sys.stdout, encoding=ENCODING):
    feed = ET.Element ("feed")
    ET.SubElement (feed, "title", type="text").text = self.title
    ET.SubElement (feed, "link", rel="self",
    if self.description:
      ET.SubElement (feed, "subtitle", type="text").text = self.description
      name, email =
      author = ET.SubElement (feed, "author")
      ET.SubElement (author, "name").text = name
      ET.SubElement (author, "email").text = email
    ET.SubElement (feed, "updated").text = self.updated_on.isoformat ()
    for entry in self.entries:
      e = ET.SubElement (feed, "entry")
      ET.SubElement (e, "title").text = entry['title']
      ET.SubElement (e, "link", href=entry['link'])
      ET.SubElement (e, "id").text = entry['id']
      ET.SubElement (e, "updated").text = entry['updated_on'].isoformat ()
      if entry['summary']:
        ET.SubElement (e, "summary").text = entry['summary']
      ET.SubElement (e, "content", type="html").text = "<![CDATA[%s]]>" % entry['content']

    ET.ElementTree (feed).write (ofile, encoding=encoding)

if __name__ == '__main__':
  feed = Feed ("Feed for", "", author=("Tim Golden", "tim at"))
  for i in range (5):
    feed.add_entry ("Entry %d" % i, "" % i, "Contents for entry %d" % i, id="entry#%d" % i)
  feed.write_atom (open ("feed.atom.xml", "w"), "iso-8859-1")
  feed.write_rss (open ("feed.rss.xml", "w"), "iso-8859-1")



More information about the Python-list mailing list