Search code examples
pythonrssfeedparser

Feedparser newbie questions


After a break from Python(and I knew very little then!) I'm coming back to it for a project(hopefully!). I want to do some parsing using Feedparser & need a few hints to start. Before anyone shouts, I have searched Google and read the docs, but I'm a bit too rusty unfortunately!(So please don't lmgtfy me!)

If I have a rss feed, then how would I parse it in order that I get each of the item titles seperately which can then be inserted into a web page?

Hope that makes sense. Many thanks for any responses.


Solution

  • import feedparser
    url = "http://..."
    feed = feedparser.parse(url)
    for post in feed.entries:
        title = post.title
        print(title)
    

    If you'd like to extract just the third post, then you could use

    post=feed.entries[2]
    

    (since python uses 0-based indexing). Printing post might be helpful; it'll show you what information is available:

    print post
    

    And finally, to grab just the title of the third post:

    print post['title']
    

    or

    print post.title