views:

63

answers:

1

Hi all,

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.

+1  A: 
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
unutbu
Have clarified above. Thanks though!
James Wanchai
@James: I'm sorry -- I don't understand what you mean by "label each title separately". Can you give an example? Maybe an rss feed so we can speak concretely?
unutbu
Each rss feed has a certain number of items, each of which are stories. Each of these items has a different title. So, if I parsed that using Feedparser, how would I use just the title of the third item? Hope that makes more sense!
James Wanchai