0

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.

James Wanchai
  • 2,861
  • 4
  • 21
  • 16

1 Answers1

1
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
  • 842,883
  • 184
  • 1,785
  • 1,677
  • Have clarified above. Thanks though! – James Wanchai Feb 23 '10 at 20:50
  • @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 Feb 23 '10 at 21:33
  • 1
    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 Feb 26 '10 at 08:55