How to use feedparser and Atoma to read RSS feeds in Python 3


GeeXLab demo - RSS feed reader with Python 3 and feedparser and Atoma



Today, let’s see how to parse an RSS feed in Python 3 with two RSS feed parser libraries:
feedparser
Atoma

Two GeeXLab demos (one with feedparser, and another one with Atoma) are available in the Python 3 demopack in the rss-reader/01-rss-reader-feedparser/ and rss-reader/02-rss-reader-atoma/ folders.

I tested both libraries on Windows 10 64-bit (v1809) with latest Python 3.7.2. These demos should also work on Linux but I didn’t tested that aspect. Feel free to post a comment here or in the forum if you encounter a problem on Linux (as well as on Windows…).

 

1 – feedparser


GeeXLab demo - RSS feed reader with Python 3 and feedparser

 
feedparser seems to be the most popular feed parser library. feedparser can parse Atom and RSS feeds in Python 3. You can download feedparser from THIS REPO.

To install feedparser, unzip the repo, open a terminal in the repo folder and type:

python setup.py install

 
Now that feedparser is installed, here is a basic use of that library:

import feedparser
url= "https://feeds2.feedburner.com/TheGeeksOf3d"
feed = feedparser.parse(url)

We have parsed Geeks3D RSS feed!

Now let’s display it by looping over all feed entries. In this code snippet, are displayed only the date, title and link of a feed entry:

for post in feed.entries:
  date = "(%d/%02d/%02d)" % (post.published_parsed.tm_year,\
    post.published_parsed.tm_mon, \
    post.published_parsed.tm_mday)
  print("post date: " + date)
  print("post title: " + post.title)
  print("post link: " + post.link)

 
feedparser documentation is available HERE.





 

2 – Atoma


GeeXLab demo - RSS feed reader with Python 3 and Atoma

 
Atoma is another library for Python 3 that deals with RSS feed parsing. Like feedparser, Atoma is simple to install and use. You can download Atoma from THIS REPOSITORY.

To install Atoma, unzip the repo, open a terminal in the repo folder and type:

python setup.py install

An optional but useful library to install alongside Atoma is Requests. Requests can be downloaded from THIS REPO.

As for other libs, unzip the repo, open a terminal in the repo folder and type:

python setup.py install

 

Now that Atoma and Requests are installed, let’s look at how to read the RSS feed of GeeXLab blog:

import atoma, requests
feed_name = "GeeXLab blog"
url = "https://www.geeks3d.com/hacklab/?feed=rss2"
response = requests.get(url)
feed = atoma.parse_rss_bytes(response.content)

We have parsed GeeXLab RSS feed! We are the best!

Now let’s see how to display the feeds:

for post in feed.items:
  date = post.pub_date.strftime('(%Y/%m/%d)')
  print("post date: " + date)
  print("post title: " + post.title)
  print("post link: " + post.link)

As you can see, displaying feeds with Atoma is pretty similar to feedparser. We are fortunate!


A note on GeeXLab demos on Windows. GeeXLab for Windows comes with a Python 3 installation. If you want to use your Python 3 installation (usually in C:/Users/YOUR_NAME/AppData/Local/Programs/Python/Python37/) with GeeXLab, you have to specify where is located the Python home. You can start GeeXLab with the /python3_home command line option:

start GeeXLab /python3_home="C:/Users/YOUR_NAME/AppData/Local/Programs/Python/Python37/"

A .bat file is available in GeeXLab root folder (start_set_python_home.bat). Edit the Python 3 home path and run it.





Leave a Comment

Your email address will not be published. Required fields are marked *