adam eliezerov adam eliezerov - 2 months ago 20
Python Question

Find a word with a for loop - python

What I'm trying to do is have python go over all the RSS feed titles and make the terminal print out only the titles with a certain word.

import feedparser

d = feedparser.parse('http://rss.cnn.com/rss/edition_technology.rss')

print d['feed']['title']
print 'number of entries: '
print len(d['entries'])
for post in d.entries:
print post.title + ": "

Answer

Your code seems great. What you are missing is an "if" which check if a specific word exists in the post.title

I've used "if" and "in" to filter only the relevant lines.

Details about "in" can be found here: http://www.jworks.nl/2013/11/07/python-goodness-the-in-keyword/

myword = "NASA" 

for post in d.entries:
  if myword in post.title:
    print post.title
Comments