forked from Akhileshsakure/RSS-feed-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed_reader.py
39 lines (33 loc) · 1.47 KB
/
feed_reader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from lib2to3.pgen2.parse import ParseError
from msilib.schema import Error
from urllib import request
import requests
import xml.etree.ElementTree as ET
def feed_read(link):
'''feed_read() parses the response and prints the blogs from the RSS feed'''
try:
res = requests.get(link)
except:
print('An error occured while parsing ' + link +', kindly check your RSS url')
else:
try:
root = ET.fromstring(res.content)
except ET.ParseError:
print('An error occured while parsing ' + link +', kindly check your RSS url')
else:
#All the items tags are stored in items
items = root[0].findall('item')
N = 5
if not len(items)>=N:
N = len(items)
#prints the title of the RSS feed
print('~~~~~~~~~ '+root[0].find('title').text.upper()+' ~~~~~~~~~')
print()
#iterates through items and prints title, description, link for each item
#Here only first five posts from each RSS feed are being printed for sake of simplicity
#and can be changed as per user's requirement by changing N
for item in items[:N]:
print('Title - ',item.find('title').text)
print('Description - ',item.find('description').text)
print('link - ',item.find('link').text)
print('\n--------------------------------------------------------------\n')