common-rss-elements.rst 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. Common :abbr:`RSS (Rich Site Summary)` Elements
  2. ===============================================
  3. The most commonly used elements in :abbr:`RSS (Rich Site Summary)` feeds
  4. (regardless of version) are title, link, description, publication date, and entry
  5. ID. The publication date comes from the pubDate element, and the entry ID comes
  6. from the guid element.
  7. This sample :abbr:`RSS (Rich Site Summary)` feed is at
  8. `http://feedparser.org/docs/examples/rss20.xml
  9. <http://feedparser.org/docs/examples/rss20.xml>`_.
  10. .. sourcecode:: xml
  11. <?xml version="1.0" encoding="utf-8"?>
  12. <rss version="2.0">
  13. <channel>
  14. <title>Sample Feed</title>
  15. <description>For documentation &lt;em&gt;only&lt;/em&gt;</description>
  16. <link>http://example.org/</link>
  17. <pubDate>Sat, 07 Sep 2002 00:00:01 GMT</pubDate>
  18. <!-- other elements omitted from this example -->
  19. <item>
  20. <title>First entry title</title>
  21. <link>http://example.org/entry/3</link>
  22. <description>Watch out for &lt;span style="background-image:
  23. url(javascript:window.location='http://example.org/')"&gt;nasty
  24. tricks&lt;/span&gt;</description>
  25. <pubDate>Thu, 05 Sep 2002 00:00:01 GMT</pubDate>
  26. <guid>http://example.org/entry/3</guid>
  27. <!-- other elements omitted from this example -->
  28. </item>
  29. </channel>
  30. </rss>
  31. The channel elements are available in ``d.feed``.
  32. Accessing Common Channel Elements
  33. ---------------------------------
  34. ::
  35. >>> import feedparser
  36. >>> d = feedparser.parse('http://feedparser.org/docs/examples/rss20.xml')
  37. >>> d.feed.title
  38. u'Sample Feed'
  39. >>> d.feed.link
  40. u'http://example.org/'
  41. >>> d.feed.description
  42. u'For documentation <em>only</em>'
  43. >>> d.feed.published
  44. u'Sat, 07 Sep 2002 00:00:01 GMT'
  45. >>> d.feed.published_parsed
  46. (2002, 9, 7, 0, 0, 1, 5, 250, 0)
  47. The items are available in ``d.entries``, which is a list. You access items in the list in the same order in which they appear in the original feed, so the first item is available in ``d.entries[0]``.
  48. Accessing Common Item Elements
  49. ------------------------------
  50. ::
  51. >>> import feedparser
  52. >>> d = feedparser.parse('http://feedparser.org/docs/examples/rss20.xml')
  53. >>> d.entries[0].title
  54. u'First item title'
  55. >>> d.entries[0].link
  56. u'http://example.org/item/1'
  57. >>> d.entries[0].description
  58. u'Watch out for <span>nasty tricks</span>'
  59. >>> d.entries[0].published
  60. u'Thu, 05 Sep 2002 00:00:01 GMT'
  61. >>> d.entries[0].published_parsed
  62. (2002, 9, 5, 0, 0, 1, 3, 248, 0)
  63. >>> d.entries[0].id
  64. u'http://example.org/guid/1'
  65. .. tip:: You can also access data from :abbr:`RSS (Rich Site Summary)` feeds using Atom terminology. See :ref:`advanced.normalization` for details.