basic-existence.rst 699 B

1234567891011121314151617181920212223242526
  1. Testing for Existence
  2. =====================
  3. Feeds in the real world may be missing elements, even elements that are
  4. required by the specification. You should always test for the existence of an
  5. element before getting its value. Never assume an element is present.
  6. To test whether elements exist, you can use standard :program:`Python`
  7. dictionary idioms.
  8. Testing if elements are present
  9. -------------------------------
  10. ::
  11. >>> import feedparser
  12. >>> d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')
  13. >>> 'title' in d.feed
  14. True
  15. >>> 'ttl' in d.feed
  16. False
  17. >>> d.feed.get('title', 'No title')
  18. u'Sample feed'
  19. >>> d.feed.get('ttl', 60)
  20. 60