File: basic-existence.rst

package info (click to toggle)
feedparser 6.0.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,540 kB
  • sloc: xml: 11,459; python: 4,575; makefile: 7
file content (26 lines) | stat: -rw-r--r-- 699 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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.

To test whether elements exist, you can use standard :program:`Python`
dictionary idioms.

Testing if elements are present
-------------------------------

::

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