views:

127

answers:

6

Hello,

I want to manipulate feed which contains frequently updated (with time) contents using feed parser. Goal is to show all the contents of the updated feed.

import feedparser
d = feedparser.parse("some URL")

print "Information of user" 
i = range(10)

for i in d:
    print d.entries[i].summary 

print " " 

As parsing data is list, and list don't accept string as indices, it shows error

like:

File "F:\JavaWorkspace\Test\src\rss_parse.py", line 18, in <module>
print d.entries[i].summary 
TypeError: list indices must be integers

Then how can I get all contents? can anyone please show me some light on this issue? Thanks in advance!

+4  A: 

i is not an integer. I guess i is already an entry of the feed but better rename it:

Try:

for entry in d.entries:
  print entry.summary

If you want the first 10 entries you have to do:

try:
  for i in range(10):
    print d.entries[i].summary
except IndexError:
  pass
Felix Kling
@felix:Actually, entries[0] is an object of feedparser module and summary is the attribute of this entries[] object. So thats why it doesnt work as you told.Do you have any other options?
rahman.bd
@felix: Sorry..but thanks for help!
rahman.bd
@felix: your tinkering doesn't improve your answer. I'm actually considering taking my upvote back, the only thing that stops me is the other answers to this question.
SilentGhost
@SilentGhost: Where is the problem if I try to correct my answer or correct my code to be more reliable? You can do whatever you want.
Felix Kling
@Felix: that's not about reliability. Iteration over the `d.entries` is the only one obvious way to do this. This whole try-range business is mank.
SilentGhost
@SilentGhost the islice function from itertools is just made to make it easy and simple without bothering with ranges. See my answer.
kriss
@SilentGhost: Ok, I got your point now. I agree. The `range` thing is only there as I don't know what the OP wanted to achieve with `range(10)`.
Felix Kling
@felix: OP clearly doesn't understand what he's doing, so I don't really think his opinion matters.
SilentGhost
+1  A: 

You first assign a list of integers to i (i = range(10)) and then just lose the reference to this list. Are you sure you didn't mean:

r = range(10)

for i in r:

or simply:

for i in range(10):
gruszczy
+4  A: 
for i in range(10):
    print d.entries[i].summary
ghostdog74
@ghostdog:Thanks... it works.. :D
rahman.bd
+1  A: 

for all entries make:

import feedparser
d = feedparser.parse("some URL")

print "Information of user" 

for i in range(len(d['entries'])):
    print d.entries[i].summary 

print " "
yanoo
Thanks..This works too!!
rahman.bd
that's just awful
SilentGhost
that is not pythonic
fabrizioM
+1  A: 
import feedparser
from StringIO import StringIO
d = feedparser.parse("some URL")
buff = StringIO()
print >>buff, "Information of user" 

for i,e in enumerate(d.entries):
    print >>buff, i, e.summary 

print >>buff," "
print buff

If you need the index, I suggest also to use a String Buffer to do I/O operations on big string.

fabrizioM
A: 

Say you want to print the 10 first elements of the list if there is 10 or more, or what it contains otherwise. Felix allready proposed a working solution with exception management. You could also use itertools like below.

import feedparser
d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')

from itertools import islice

for elt in islice(d.entries, 1, 10):
    print elt.summary

What is nice with islice is that if you want to access to elements from say 3 to 10 (a slice) it also works as easily. Just have to replace 1 with 3. It also works with step if you want say only even elements, etc.

kriss
Well, then what if I do not know before hand the limit of feed content, as this feed is updating every after 10 mins..and I want all these by using this islice?
rahman.bd
@rahman.bd: You can use None as a boundary if you do not want any limit. As islice is an iterator you will still access to the original object and it's not different when you use an explicit index. But it seems that in the case you describe you would not use an islice at all more likely nothing like proposed Felix, or an ifilter if you are only interested with some of the feeds.
kriss