content-normalization.rst 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. .. _advanced.normalization:
  2. Content Normalization
  3. =====================
  4. :program:`Universal Feed Parser` can parse many different types of feeds: Atom,
  5. :abbr:`CDF (Channel Definition Format)`, and nine different versions of
  6. :abbr:`RSS (Rich Site Summary)`. You should not be forced to learn the
  7. differences between these formats. :program:`Universal Feed Parser` does its
  8. best to ensure that you can treat all feeds the same way, regardless of format
  9. or version.
  10. You can access the basic elements of an Atom feed using :abbr:`RSS (Rich Site Summary)` terminology.
  11. Accessing an Atom feed as an :abbr:`RSS (Rich Site Summary)` feed
  12. -----------------------------------------------------------------
  13. ::
  14. >>> import feedparser
  15. >>> d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')
  16. >>> d['channel']['title']
  17. u'Sample Feed'
  18. >>> d['channel']['link']
  19. u'http://example.org/'
  20. >>> d['channel']['description']
  21. u'For documentation <em>only</em>
  22. >>> len(d['items'])
  23. 1
  24. >>> e = d['items'][0]
  25. >>> e['title']
  26. u'First entry title'
  27. >>> e['link']
  28. u'http://example.org/entry/3'
  29. >>> e['description']
  30. u'Watch out for nasty tricks'
  31. >>> e['author']
  32. u'Mark Pilgrim (mark@example.org)'
  33. The same thing works in reverse: you can access :abbr:`RSS (Rich Site Summary)` feeds as if they were Atom feeds.
  34. Accessing an :abbr:`RSS (Rich Site Summary)` feed as an Atom feed
  35. -----------------------------------------------------------------
  36. ::
  37. >>> import feedparser
  38. >>> d = feedparser.parse(' http://feedparser.org/docs/examples/rss20.xml')
  39. >>> d.feed.subtitle_detail
  40. {'type': 'text/html',
  41. 'base': 'http://feedparser.org/docs/examples/rss20.xml',
  42. 'language': None,
  43. 'value': u'For documentation <em>only</em>'}
  44. >>> len(d.entries)
  45. 1
  46. >>> e = d.entries[0]
  47. >>> e.links
  48. [{'rel': 'alternate',
  49. 'type': 'text/html',
  50. 'href': u'http://example.org/item/1'}]
  51. >>> e.summary_detail
  52. {'type': 'text/html',
  53. 'base': 'http://feedparser.org/docs/examples/rss20.xml',
  54. 'language': u'en',
  55. 'value': u'Watch out for <span>nasty tricks</span>'}
  56. >>> e.updated_parsed
  57. (2002, 9, 5, 0, 0, 1, 3, 248, 0)
  58. .. note::
  59. For more examples of how :program:`Universal Feed Parser` normalizes
  60. content from different formats, see :ref:`annotated`.