how to test attribute existence of feedparser objects

xDog Walker thudfoo at gmail.com
Sat Dec 10 12:19:31 EST 2011


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.




More information about the Python-list mailing list