Introduction to Python¶
SOLUTION¶
#!/usr/bin/env python
# -*- encoding: utf-8
import feedparser
# catch the RSS feed
url = "http://montrealpython.org/feed/"
feed = feedparser.parse(url)
# keep the desired number of items
#items = feed['items'] # all
items = feed['items'][0:5] # 5 last items because already sorted by .updated
# treat the selected items
reply = u"The 5 last modifications on Montréal-Python's website :"
for i in items:
# reply = unicode string where placeholders %s are substituted by the values in the tuple
#reply = reply + u'\n%s : %s (%s)' % (i['link'], i['title'], i['updated'])
reply = reply + u'\n%s : %s' % (i['link'], i['title'])
print reply
More compact, without the comments :
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import feedparser
url = "http://montrealpython.org/feed/"
feed = feedparser.parse(url)
items = feed['items'][0:5] # 5 last items because already sorted by .updated
reply = u"The 5 last modifications on Montréal-Python's website :"
for i in items:
#reply = reply + u'\n%s : %s (%s)' % (i['link'], i['title'], i['updated'])
reply = reply + u'\n%s : %s' % (i['link'], i['title'])
print reply
More pythonic :
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import feedparser
url = "http://montrealpython.org/feed/"
feed = feedparser.parse(url)
print = u"The 5 last modifications on Montréal-Python's website :"
for e in feed.entries[:5]:
print u'\n%s : %s' % (e.link, e.title)