Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to test attribute existence of feedparser objects

155 views
Skip to first unread message

HansPeter

unread,
Dec 8, 2011, 4:34:09 AM12/8/11
to
Hi,

While using the feedparser library for downloading RSS feeds some of
the blog entries seem to have no title.

File "build\bdist.win32\egg\feedparser.py", line 382, in __getattr__
AttributeError: object has no attribute 'title'

Is there a way to test the existence of an attribute?

I can use an exception but like below to see whether it exists but
this is a clumsy way since the function has to return the title.

d=feedparser.parse(url,handlers = [proxy])
try:
print "TITLE ",d.feed.title
except:
print "HAS NO TITLE"
wc={}

Regards HansPeter

Chris Rebert

unread,
Dec 8, 2011, 5:24:30 AM12/8/11
to HansPeter, pytho...@python.org
On Thu, Dec 8, 2011 at 1:34 AM, HansPeter <hanspet...@gmail.com> wrote:
> Hi,
>
> While using the feedparser library for downloading RSS feeds some of
> the blog entries seem to have no title.
>
>  File "build\bdist.win32\egg\feedparser.py", line 382, in __getattr__
> AttributeError: object has no attribute 'title'
>
> Is there a way to test the existence of an attribute?
>
> I can use an exception but like below to see whether it exists but
> this is a clumsy way since the function has to return the title.

hasattr(obj, attr_name)
See docs.python.org/dev/library/functions.html#hasattr

That said, sounds like it won't make much difference in the particular
case you mention.
Also, never use a bare "except:" clause, unless you know what you're
doing and have a really good reason. Do "except AttributeError" in
this case.

Cheers,
Chris
--
http://rebertia.com

xDog Walker

unread,
Dec 10, 2011, 12:19:31 PM12/10/11
to pytho...@python.org
On Thursday 2011 December 08 01:34, HansPeter wrote:
> Hi,
>
> While using the feedparser library for downloading RSS feeds some of
> the blog entries seem to have no title.
>
> File "build\bdist.win32\egg\feedparser.py", line 382, in __getattr__
> AttributeError: object has no attribute 'title'
>
> Is there a way to test the existence of an attribute?
>

From the Fine Manual for feedparser 5.1:

Testing for Existence¶

Feeds in the real world may be missing elements, even elements that are
required by the specification. You should always test for the existence of an
element before getting its value. Never assume an element is present.

Use standard Python dictionary functions such as has_key to test whether an
element exists.
Testing if elements are present¶

>>> import feedparser
>>> d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')
>>> d.feed.has_key('title')
True
>>> d.feed.has_key('ttl')
False
>>> d.feed.get('title', 'No title')
u'Sample feed'
>>> d.feed.get('ttl', 60)
60


--
I have seen the future and I am not in it.

Steven D'Aprano

unread,
Dec 10, 2011, 12:51:16 PM12/10/11
to
On Sat, 10 Dec 2011 09:19:31 -0800, xDog Walker wrote:

> Use standard Python dictionary functions such as has_key to test whether
> an element exists.

Idiomatic Python code today no longer uses has_key.

# Was:
d.feed.has_key('title')

# Now preferred
'title' in d.feed



--
Steven
0 new messages