All Courses

Python - RSS Feed

Anand Roy

a year ago

Read RSS feed in Python | InsideAIML
Table of Contents
  • Introduction
  • Feed Structure
  • Feed Title and Posts
  • Feed Details

Introduction

          RSS (Rich Site Summary) is a format for delivering regularly changing web content. Many news-related sites, weblogs, and other online publishers syndicate their content as an RSS Feed to whoever wants it. In python, we take the help of the below package to read and process these feeds.
pip install feedparser

Feed Structure

          In the below example we get the structure of the feed so that we can analyze further about which parts of the feed we want to process.
import feedparser
NewsFeed = feedparser.parse("https://timesofindia.indiatimes.com/rssfeedstopstories.cms")
entry = NewsFeed.entries[1]

print(entry.keys())
When we run the above program, we get the following output
dict_keys(['title', 'title_detail', 'summary', 'summary_detail', 'links', 'link', 'id', 'guidislink', 'published', 'published_parsed'])

Feed Title and Posts

          In the below example we read the title and head of the RSS feed.

import feedparser

NewsFeed = feedparser.parse("https://timesofindia.indiatimes.com/rssfeedstopstories.cms")

print('Number of RSS posts :', len(NewsFeed.entries))

entry = NewsFeed.entries[1]
print('Post Title :',entry.title)
When we run the above program we get the following output
Number of RSS posts : 19
Post Title : Live: Section 144 in Hathras ahead of Rahul's visit

Feed Details

         Based on the above entry structure we can derive the necessary details from the feed using the python program as shown below. As entry is a dictionary we utilize its keys to produce the values needed.

import feedparser

NewsFeed = feedparser.parse("https://timesofindia.indiatimes.com/rssfeedstopstories.cms")

entry = NewsFeed.entries[1]

print entry.published
print("******")
print(entry.summary)
print("------News Link--------")
print(entry.link)
When we run the above program we get the following output
Tue, 29 Sep 2020 16:21:33 IST
******
A day after Uttar Pradesh chief minister Yogi Adityanath spoke to the victim's family and assured strict punishment to the culprits, the 3-member SIT visited the village and launched its probe on Thursday. Meanwhile, Hathras district administration has imposed Section 144 in the district. Stay with TOI for all the latest updates:
------News Link--------
https://timesofindia.indiatimes.com/city/delhi/live-updates-hathras-gangrape-victim-dies-in-delhi-hospital/liveblog/78384037.cms
Note: While running the above code you may get some different output.
I hope you enjoyed reading this article and finally, you came to know about Python - RSS Feed.
      
Enjoyed reading this blog? Then why not share it with others. Help us make this AI community stronger. 
To learn more about such concepts related to Artificial Intelligence, visit our insideAIML blog page.
You can also ask direct queries related to Artificial Intelligence, Deep Learning, Data Science and Machine Learning on our live insideAIML discussion forum.
Keep Learning. Keep Growing.

Submit Review