I’ve made some progress with the cElementTree
Python module. After reading the available documentation and the RSS 2.0 specification, I was able to write a very simplistic RSS 2.0 feed generator in order to demonstrate the usage of this module. There is no such example in the documentation, so this might be useful to someone.
The following code saves the generated XML code to a file, named RSS2example.xml
, in the script’s directory:
import cElementTree as ET def Construct_Feed( Stories ): RSSroot = ET.Element( 'rss', {'version':'2.0'} ) RSSchannel = ET.SubElement( RSSroot, 'channel' ) ET.SubElement( RSSchannel, 'title' ).text = 'Sample RSS2 Feed' ET.SubElement( RSSchannel, 'link' ).text = 'http://www.example.com/rss2.xml' ET.SubElement( RSSchannel, 'description' ).text = 'Sample RSS2 feed generated by the cElementTree Python module' for Story in Stories: RSSitem = ET.SubElement ( RSSchannel, 'item' ) ET.SubElement( RSSitem, 'title' ).text = Story[0] ET.SubElement( RSSitem, 'description' ).text = Story[1] RSSfeed = ET.ElementTree(RSSroot) RSSfeed.write("RSS2example.xml") if __name__=='__main__': Stories = [ ( 'Story 1', 'This is the description of story 1' ), ( 'Story 2', 'This is the description of story 2' ), ( 'Story 3', 'This is the description of story 3' ), ( 'Story 4', 'This is the description of story 4' ), ( 'Story 5', 'This is the description of story 5' ) ] Construct_Feed( Stories )
The sample feed contains 5 stories, just a title and a description. The above code is free of extra XML namespaces and comments. Also, it would be possible to include the linefeed
and tab
characters inside the text
and tail
element attributes in order to produce more easily readable output. But, I deliberately decided to strip them before posting, because they might make the script seem complex.
I will post a complete RSS 2.0 feed generator script when I have the time to write it.
Creating An RSS 2.0 Feed Using cElementTree by George Notaras is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Copyright © 2006 - Some Rights Reserved
try using ATOM though
Yeah, I consider Atom as a more flexible feed format too. As you can see, I have taken the time to convert the deprecated WordPress Atom 0.3 feed to comply with the Atom 1.0 specification.
But, finally, the real problem with it is if the majority of the web services fully support it. To my surprise, Google still supports only the Atom 0.3 format. Technorati seems to support Atom 1.0. The feed validator accepts only Atom 1.0. I don’t have a clue about other web services.
This seems like a mess, so I tend to stick with RSS2 or RDF feeds for the moment.
okay i have no idea on python programming. but i love to create better RSS feeds on my blog. is python an easy language to master? :P