Python - RSS Feed

Anand Roy

8 months ago

Read RSS feed in Python | Insideaiml
Read RSS feed in Python | Insideaiml
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.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review