RSS (Really Simple Syndication) Feed Reader in Python

RSS (Really Simple Syndication) feeds are a popular way to get updates from websites in a standardized format. An RSS feed reader can fetch and display the latest entries from an RSS feed, allowing users to stay up-to-date with their favorite websites. In this example, we’ll create a Python script that reads an RSS feed from a given URL and prints the titles and links of the latest entries. We will use the feedparser library to parse the RSS feed.

Code Example

import feedparser

def read_rss_feed(feed_url):
    feed = feedparser.parse(feed_url)
    if feed.bozo:
        print("Failed to parse feed. Please check the URL.")
        return

    print(f"Feed Title: {feed.feed.title}")
    print(f"Feed Link: {feed.feed.link}")
    print("\nLatest Entries:\n")

    for entry in feed.entries[:10]:  # Display only the latest 10 entries
        print(f"Title: {entry.title}")
        print(f"Link: {entry.link}\n")

def main():
    print("Welcome to the RSS Feed Reader!")
    feed_url = input("Enter the RSS feed URL: ").strip()
    read_rss_feed(feed_url)

if __name__ == "__main__":
    main()

Detailed Code Explanation

Importing Required Module

import feedparser
  • feedparser: This module is used to parse RSS and Atom feeds. It handles the feed parsing and provides a structured way to access the feed’s contents.

RSS Feed Reading Function

def read_rss_feed(feed_url):
    feed = feedparser.parse(feed_url)
    if feed.bozo:
        print("Failed to parse feed. Please check the URL.")
        return

    print(f"Feed Title: {feed.feed.title}")
    print(f"Feed Link: {feed.feed.link}")
    print("\nLatest Entries:\n")

    for entry in feed.entries[:10]:  # Display only the latest 10 entries
        print(f"Title: {entry.title}")
        print(f"Link: {entry.link}\n")
  1. Function Definition: read_rss_feed(feed_url)
    • feed_url: URL of the RSS feed to be read.
  2. Parse RSS Feed: feed = feedparser.parse(feed_url)
    • Use feedparser.parse() to fetch and parse the RSS feed from the given URL.
  3. Error Handling:
    • if feed.bozo: checks if there was a problem parsing the feed (e.g., invalid URL or malformed feed).
    • If there’s an error, print a message and exit the function.
  4. Print Feed Information:
    • Print the feed’s title and link using feed.feed.title and feed.feed.link.
  5. Print Latest Entries:
    • Loop through the first 10 entries in the feed using for entry in feed.entries[:10]:.
    • Print the title and link of each entry using entry.title and entry.link.

Main Function

def main():
    print("Welcome to the RSS Feed Reader!")
    feed_url = input("Enter the RSS feed URL: ").strip()
    read_rss_feed(feed_url)
  1. Greeting: Print a welcome message.
  2. User Input for RSS Feed URL:
    • Prompt the user to enter the URL of the RSS feed they want to read.
    • Strip any leading or trailing whitespace from the input.
  3. Call RSS Reading Function:
    • read_rss_feed(feed_url)
    • Call the read_rss_feed() function with the provided URL.