common-atom-elements.rst 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. Common Atom Elements
  2. ====================
  3. Atom feeds generally contain more information than :abbr:`RSS (Rich Site Summary)`
  4. feeds (because more elements are required), but the most commonly used elements
  5. are still title, link, subtitle/description, various dates, and ID.
  6. This sample Atom feed is at `http://feedparser.org/docs/examples/atom10.xml
  7. <http://feedparser.org/docs/examples/atom10.xml>`_.
  8. .. sourcecode:: xml
  9. <?xml version="1.0" encoding="utf-8"?>
  10. <feed xmlns="http://www.w3.org/2005/Atom"
  11. xml:base="http://example.org/"
  12. xml:lang="en">
  13. <title type="text">Sample Feed</title>
  14. <subtitle type="html">
  15. For documentation &lt;em&gt;only&lt;/em&gt;
  16. </subtitle>
  17. <link rel="alternate" href="/"/>
  18. <link rel="self"
  19. type="application/atom+xml"
  20. href="http://www.example.org/atom10.xml"/>
  21. <rights type="html">
  22. &lt;p>Copyright 2005, Mark Pilgrim&lt;/p>&lt;
  23. </rights>
  24. <id>tag:feedparser.org,2005-11-09:/docs/examples/atom10.xml</id>
  25. <generator
  26. uri="http://example.org/generator/"
  27. version="4.0">
  28. Sample Toolkit
  29. </generator>
  30. <updated>2005-11-09T11:56:34Z</updated>
  31. <entry>
  32. <title>First entry title</title>
  33. <link rel="alternate"
  34. href="/entry/3"/>
  35. <link rel="related"
  36. type="text/html"
  37. href="http://search.example.com/"/>
  38. <link rel="via"
  39. type="text/html"
  40. href="http://toby.example.com/examples/atom10"/>
  41. <link rel="enclosure"
  42. type="video/mpeg4"
  43. href="http://www.example.com/movie.mp4"
  44. length="42301"/>
  45. <id>tag:feedparser.org,2005-11-09:/docs/examples/atom10.xml:3</id>
  46. <published>2005-11-09T00:23:47Z</published>
  47. <updated>2005-11-09T11:56:34Z</updated>
  48. <summary type="text/plain" mode="escaped">Watch out for nasty tricks</summary>
  49. <content type="application/xhtml+xml" mode="xml"
  50. xml:base="http://example.org/entry/3" xml:lang="en-US">
  51. <div xmlns="http://www.w3.org/1999/xhtml">Watch out for
  52. <span style="background: url(javascript:window.location='http://example.org/')">
  53. nasty tricks</span></div>
  54. </content>
  55. </entry>
  56. </feed>
  57. The feed elements are available in ``d.feed``.
  58. Accessing Common Feed Elements
  59. ------------------------------
  60. ::
  61. >>> import feedparser
  62. >>> d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')
  63. >>> d.feed.title
  64. u'Sample feed'
  65. >>> d.feed.link
  66. u'http://example.org/'
  67. >>> d.feed.subtitle
  68. u'For documentation <em>only</em>'
  69. >>> d.feed.updated
  70. u'2005-11-09T11:56:34Z'
  71. >>> d.feed.updated_parsed
  72. (2005, 11, 9, 11, 56, 34, 2, 313, 0)
  73. >>> d.feed.id
  74. u'tag:feedparser.org,2005-11-09:/docs/examples/atom10.xml'
  75. Entries are available in ``d.entries``, which is a list. You access entries in
  76. the order in which they appear in the original feed, so the first entry is
  77. ``d.entries[0]``.
  78. Accessing Common Entry Elements
  79. -------------------------------
  80. ::
  81. >>> import feedparser
  82. >>> d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')
  83. >>> d.entries[0].title
  84. u'First entry title'
  85. >>> d.entries[0].link
  86. u'http://example.org/entry/3
  87. >>> d.entries[0].id
  88. u'tag:feedparser.org,2005-11-09:/docs/examples/atom10.xml:3'
  89. >>> d.entries[0].published
  90. u'2005-11-09T00:23:47Z'
  91. >>> d.entries[0].published_parsed
  92. (2005, 11, 9, 0, 23, 47, 2, 313, 0)
  93. >>> d.entries[0].updated
  94. u'2005-11-09T11:56:34Z'
  95. >>> d.entries[0].updated_parsed
  96. (2005, 11, 9, 11, 56, 34, 2, 313, 0)
  97. >>> d.entries[0].summary
  98. u'Watch out for nasty tricks'
  99. >>> d.entries[0].content
  100. [{'type': u'application/xhtml+xml',
  101. 'base': u'http://example.org/entry/3',
  102. 'language': u'en-US',
  103. 'value': u'<div>Watch out for <span>nasty tricks</span></div>'}]
  104. .. note::
  105. The parsed summary and content are not the same as they appear in the
  106. original feed. The original elements contained dangerous :abbr:`HTML
  107. (HyperText Markup Language)` markup which was sanitized. See
  108. :ref:`advanced.sanitization` for details.
  109. Because Atom entries can have more than one content element,
  110. ``d.entries[0].content`` is a list of dictionaries. Each dictionary contains
  111. metadata about a single content element. The two most important values in the
  112. dictionary are the content type, in ``d.entries[0].content[0].type``, and the
  113. actual content value, in ``d.entries[0].content[0].value``.
  114. You can get this level of detail on other Atom elements too.