0

Using Feedparser to parse multiple RSS feeds but this function doesn't work. How can I create a function to parse feeds and get the link out of the feed entry to further push to a tweet?

def get():
    rss_url = [
    'http://www.huffingtonpost.com/news/syria/feed/',
    'http://www.nytimes.com/svc/collections/v1/publish  /www.nytimes.com/topic/destination/syria/rss.xml',
    ]

def getHeadlines(rss_url):
    feeds = []
    for url in rss_url:
        feeds.append(feedparser.parse(url))

    for feed in feeds:
        for post in feed.entries:
            return post.link

tweet(getHeadlines(rss_url))

I am able to do it when I try just this -

RSS_URLS = [
'http://feeds.feedburner.com/RockPaperShotgun',
'http://www.gameinformer.com/b/MainFeed.aspx?Tags=preview',
]

feed = feedparser.parse(RSS_URLS)

for post in feed.entries:
    print post.title
rlandster
  • 7,294
  • 14
  • 58
  • 96
  • What does "to no avail" mean in this context? In what way does this code not do what you expect? Please read [ask] for tips on asking effective questions; this is the single biggest thing you can do to increase your chances of getting a good answer. – ChrisGPT was on strike Apr 16 '17 at 17:25
  • I expect the code to parse the feeds and derive the link of each entry in the feed. It is not doing that when I define a function. – codenovice Apr 16 '17 at 17:32
  • Please fix the indentation of your code block. Right now it isn't clear what lines are part of `getHeadlines()` and what lines aren't. – ChrisGPT was on strike Apr 16 '17 at 17:34
  • Fixed. Hope it's better now? – codenovice Apr 16 '17 at 17:38

1 Answers1

0

returning in a loop doesn't cause a list of things to be returned; it causes the function to return immediately.

In this case you will only ever get the result of the first loop iteration (the first link from the first post in the first feed). Have a look at the return value of your current getHeadlines(rss_url) to see what I mean.

Instead, you could build a list of links similarly to how you are building a list of feeds and return that:

links = []
for feed in feeds:
    for post in feed.entries:
        links.append(post.link)

return links

I'm not sure what tweet() is supposed to be, but you'll probably have to tweet each element in your list individually.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257