Update your RSS with Python


In this post I will show you a simple Python script which goal is update an existent RSS Atom file with the content of a new post.

This solves a problem of mine: I couldn't find a simple, stupid script to do this basic thing. You have to consider that many websites use platforms like Hugo or Write.as, which have their own plugins to auto-update RSS feeds.

How a RSS file should look

This may sound trivial, but it's not. To have an idea about how RSS files are done, I just looked at the feeds I follow using Miniflux. Here is the funny thing: I could not find two files structured in the same way. Sure, they were all RSS files, but with so many differences!

There exist a lot of variants of RSS, with the version 2.0 which looks to be prevailing. However, also Atom exists.

Let's look at the feeds I found. One was like this:

<rss version="2.0">
    <atom:link rel="hub" href="http://feedpress.superfeedr.com/"/>
    <atom:link href="https://example.com" rel="self" type="application/rss+xml"/>
    <pubDate>Fri, 22 Jul 2018 05:26:43 -0400</pubDate>
    <lastBuildDate>Fri, 22 Jul 2018 05:26:43 -0400</lastBuildDate>
      <pubDate>Thu, 01 Jun 2017 00:00:00 -0400</pubDate>
      <guid isPermaLink="false">

This looks like a standard RSS; however, we have more entries related to Feedpress, a paid service to manage your RSS feeds.

Okay, I don't need this, I can manage my RSS by myself, thank you. Let's see the next:

<rss version="2.0">
    <generator>Hugo -- gohugo.io</generator>
    <lastBuildDate>Sun, 21 May 2020 10:26:31 -0600</lastBuildDate>
    <atom:link href="https://example.com/index.xml" rel="self" type="application/rss+xml"/>
      <pubDate>Sat, 15 May 2020 03:02:34 -0700</pubDate>

This one sounds more standard. However, it has been generated with Hugo, as we can see from the extra generator field. Lastly, let's give a look at one Atom:

  <link href="http://example.com/atom.xml" rel="self"/>
  <link href="http://example.com"/>
    <name>Name Surname</name>
  <generator uri="http://gohugo.io/">Hugo</generator>
    <title type="html">Title</title>
    <link href="https://example.com/post"/>
    <content type="html">

The first differences I can see are:

  1. The structure is big container feed and every post is an entry;
  2. We have some different fields like author, id;
  3. The date is in another format.

The first two differences were not so problemat; but the last was, because dealing with date is always a pain in programming.

Let's get it clean

So, I looked at the structure of RSS as defined by the W3C. The basic structure should be something like:

<rss version="2.0">
    <title>AlexMV12's blog</title>
    <description> New posts on AlexMV12's blog. </description>
    <atom:link href="https://alexmv12.xyz/atom.xml" rel="self" type="application/rss+xml"/>
      <title>My new website</title>
      <link> https://alexmv12.xyz/blog/newblog </link>
      <pubDate> Tue, 02 Jun 2020 17:00:34 +0200 </pubDate>
      <guid> https://alexmv12.xyz/blog/newblog </guid>

This is valid RSS, as suggested by the W3C Feed Validation Service. You can see, however, the presence of a atom:link field. This was suggested by the validator itself.

A simple Python script

import argparse
from bs4 import BeautifulSoup
from email.utils import formatdate

def main(starting_rss_path, html_path, new_link, output_path):
    # First, we open the existent RSS file.
    handler = open(starting_rss_path).read()
    soup = BeautifulSoup(handler, features="lxml-xml")
    # ... and we get the channel section:
    channel = soup.channel

    # Prepare the new post's fields. For now use new_tag on the soup
    # objects, we will put this in the right position later.
    new_post = soup.new_tag('item')
    title = soup.new_tag('title')
    link = soup.new_tag('link')
    pubDate = soup.new_tag('pubDate')
    guid = soup.new_tag('guid')
    description = soup.new_tag('description')

    # Populate the new post by reading the correct data
    # from the HTML page.
    handler = open(html_path).read()
    parsed_html = BeautifulSoup(handler, features="lxml")

    # content is the div with id "content", where the actual post is written.
    # You should replace this with the appropriate selector.
    content = parsed_html.body.find('div', attrs={'id': 'content'})

    # we use the first h1 as title for the post
    title.string = content.find('h1').text

    # use the link passed as parameter
    link.string = guid.string = new_link

    pubDate.string = formatdate(localtime=True)

    # this is the real content of the post. Join all the rows.
    description.string = ''.join(map(str, content.contents))

    # put the elements in order.


    # pretty print the XML
    prettified_xml = soup.prettify()

    with open(output_path, 'w') as file:

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Simple RSS builder.')
    parser.add_argument("-s", "--starting_rss_file",
                        help="Path of the starting RSS file; it has to be a valid RSS xml file.")
    parser.add_argument("-i", "--input",
                        help="HTML file to extract content from")
    parser.add_argument("-l", "--link",
                        help="Link that the post will have once published")
    parser.add_argument("-o", "--output",
                        help="Path of the new RSS file")
    args = parser.parse_args()
    main(args.starting_rss_file, args.input, args.link, args.output)

This is what I came up with.

We can launch it using:

python rss_creator.py -s "atom.xml" -i "new_post.html" -l "https://example.com/blog/post1" -o "new_rss.xml"


  1. "atom.xml" is the path of an already existent RSS feed; it should have at least channel;
  2. "new_post.html" is the path of the HTML file which contains our new post, and from which we will extract the content;
  3. "https://example.com/blog/post1" is the link that our post will have; I haven't found a way to set it automatically;
  4. "new_rss.xml" is the path of our new RSS file.

The only library used is BeautifulSoup. I hope the comments in the code clarify it enough.