character-encoding.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. .. _advanced.encoding:
  2. Character Encoding Detection
  3. ============================
  4. .. tip::
  5. Feeds may be published in any character encoding. :program:`Python`
  6. supports only a few character encodings by default. To support the maximum
  7. number of character encodings (and be able to parse the maximum number of
  8. feeds), you should install :file:`cjkcodecs` and :file:`iconv_codec`. Both are
  9. available at `http://cjkpython.i18n.org/ <http://cjkpython.i18n.org/>`_.
  10. `RFC 3023 <http://www.ietf.org/rfc/rfc3023.txt>`_ defines the interaction
  11. between :abbr:`XML (Extensible Markup Language)` and :abbr:`HTTP (Hypertext Transfer Protocol)`
  12. as it relates to character encoding. :abbr:`XML (Extensible Markup Language)`
  13. and :abbr:`HTTP (Hypertext Transfer Protocol)` have different ways of
  14. specifying character encoding and different defaults in case no encoding is
  15. specified, and determining which value takes precedence depends on a variety of
  16. factors.
  17. Introduction to Character Encoding
  18. ----------------------------------
  19. In :abbr:`XML (Extensible Markup Language)`, the character encoding is optional
  20. and may be given in the :abbr:`XML (Extensible Markup Language)` declaration in
  21. the first line of the document, like this:
  22. .. sourcecode:: xml
  23. <?xml version="1.0" encoding="utf-8"?>
  24. If no encoding is given, :abbr:`XML (Extensible Markup Language)` supports the
  25. use of a Byte Order Mark to identify the document as some flavor of UTF-32,
  26. UTF-16, or UTF-8. `Section F of the XML specification <http://www.w3.org/TR/REC-xml/#sec-guessing-no-ext-info>`_
  27. outlines the process for determining the character encoding based on unique
  28. properties of the Byte Order Mark in the first two to four bytes of the
  29. document.
  30. If no encoding is specified and no Byte Order Mark is present, :abbr:`XML (Extensible Markup Language)`
  31. defaults to UTF-8.
  32. :abbr:`HTTP (Hypertext Transfer Protocol)` uses :abbr:`MIME` to define a method
  33. of specifying the character encoding, as part of the Content-Type :abbr:`HTTP (Hypertext Transfer Protocol)`
  34. header, which looks like this:
  35. ::
  36. Content-Type: text/html; charset="utf-8"
  37. If no charset is specified, :abbr:`HTTP (Hypertext Transfer Protocol)` defaults
  38. to iso-8859-1, but only for text/* media types. For other media types, the
  39. default encoding is undefined, which is where :abbr:`RFC (Request For Comments)` 3023 comes in.
  40. According to :abbr:`RFC (Request For Comments)` 3023, if the media type given
  41. in the Content-Type :abbr:`HTTP (Hypertext Transfer Protocol)` header is
  42. application/xml, application/xml-dtd, application/xml-external-parsed-entity,
  43. or any one of the subtypes of application/xml such as application/atom+xml or
  44. application/rss+xml or even application/rdf+xml, then the encoding is
  45. #. the encoding given in the ``charset`` parameter of the Content-Type :abbr:`HTTP (Hypertext Transfer Protocol)` header, or
  46. #. the encoding given in the encoding attribute of the :abbr:`XML (Extensible Markup Language)` declaration within the document, or
  47. #. utf-8.
  48. On the other hand, if the media type given in the Content-Type
  49. :abbr:`HTTP (Hypertext Transfer Protocol)` header is text/xml,
  50. text/xml-external-parsed-entity, or a subtype like text/AnythingAtAll+xml, then
  51. the encoding attribute of the :abbr:`XML (Extensible Markup Language)`
  52. declaration within the document is ignored completely, and the encoding is
  53. #. the encoding given in the charset parameter of the Content-Type :abbr:`HTTP (Hypertext Transfer Protocol)` header, or
  54. #. us-ascii.
  55. Handling Incorrectly-Declared Encodings
  56. ---------------------------------------
  57. :program:`Universal Feed Parser` initially uses the rules specified in
  58. :abbr:`RFC (Request For Comments)` 3023 to determine the character encoding of
  59. the feed. If parsing succeeds, then that's that. If parsing fails,
  60. :program:`Universal Feed Parser` sets the ``bozo`` bit to ``1`` and sets
  61. ``bozo_exception`` to ``feedparser.CharacterEncodingOverride``. Then it tries
  62. to reparse the feed with the following character encodings:
  63. #. the encoding specified in the :abbr:`XML (Extensible Markup Language)` declaration
  64. #. the encoding sniffed from the first four bytes of the document (as per `Section F <http://www.w3.org/TR/REC-xml/#sec-guessing-no-ext-info>`_)
  65. #. the encoding auto-detected by the `Universal Encoding Detector <http://chardet.feedparser.org/>`_, if installed
  66. #. utf-8
  67. #. windows-1252
  68. If the character encoding can not be determined, :program:`Universal Feed Parser`
  69. sets the ``bozo`` bit to ``1`` and sets ``bozo_exception`` to
  70. ``feedparser.CharacterEncodingUnknown``. In this case, parsed values will be
  71. strings, not Unicode strings.
  72. Handling Incorrectly-Declared Media Types
  73. -----------------------------------------
  74. :abbr:`RFC (Request For Comments)` 3023 only applies when the feed is served
  75. over :abbr:`HTTP (Hypertext Transfer Protocol)` with a Content-Type that
  76. declares the feed to be some kind of :abbr:`XML (Extensible Markup Language)`.
  77. However, some web servers are severely misconfigured and serve feeds with a
  78. Content-Type of text/plain, application/octet-stream, or some completely bogus
  79. media type.
  80. :program:`Universal Feed Parser` will attempt to parse such feeds, but it will
  81. set the ``bozo`` bit to ``1`` and set ``bozo_exception`` to
  82. ``feedparser.NonXMLContentType``.
  83. .. seealso::
  84. * `RFC 3023 <http://www.ietf.org/rfc/rfc3023.txt>`_
  85. * `Section F of the XML specification <http://www.w3.org/TR/REC-xml/#sec-guessing-no-ext-info>`_
  86. * `On the well-formedness of XML documents served as text/plain <http://www.imc.org/atom-syntax/mail-archive/msg05575.html>`_
  87. * `CJKCodecs and iconv_codec <http://cjkpython.i18n.org/>`_