Skip to content

Document summary, title and more are not accessible with input bytes content #194

Closed

Description

@tomdpsrd

Actual Behavior

Document.summary() is not working with python3 when the document is based on bytes and not on string content.

Steps to Reproduce the Problem

Follow the readme steps

>>> import requests
>>> from readability import Document

>>> response = requests.get('http://example.com')
>>> doc = Document(response.content)
>>> doc.title()
Traceback (most recent call last):
...
    RE_CHARSET.findall(page) + RE_PRAGMA.findall(page) + RE_XML.findall(page)
    ^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: cannot use a string pattern on a bytes-like object

How to correct

String Regexp should be updated ro bytes regexp since encoding.get_encoding is only used for bytes content.
In encoding.py :

RE_CHARSET = re.compile(br'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I)
RE_PRAGMA = re.compile(br'<meta.*?content=["\']*;?charset=(.+?)["\'>]', flags=re.I)
RE_XML = re.compile(br'^<\?xml.*?encoding=["\']*(.+?)["\'>]')

Activity

  1. tomdpsrd commented on Jul 28, 2025

    @tomdpsrd
    Author

    @buriy to check and merge if it suits you

  2. BruteThought commented on Jun 11, 2026

    @BruteThought

    Any update on this?

  3. Medno commented on Jun 24, 2026

    @Medno

    @buriy What do you think about this implementation ?

  4. buriy commented on Jun 25, 2026

    @buriy
    Owner

    Sure let's fix this, bytes weren't supposed to be there but I support all mass wanted features

  5. speedyk-005 commented on Jul 26, 2026

    @speedyk-005

    Same here

    error getting summary:
    Traceback (most recent call last):
      File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.13/site-packages/readability/readability.py", line 227, in summary
        self._html(True)
        ~~~~~~~~~~^^^^^^
      File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.13/site-packages/readability/readability.py", line 153, in _html
        self.html = self._parse(self.input)
                    ~~~~~~~~~~~^^^^^^^^^^^^
      File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.13/site-packages/readability/readability.py", line 166, in _parse
        doc, self.encoding = build_doc(input)
                             ~~~~~~~~~^^^^^^^
      File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.13/site-packages/readability/htmls.py", line 16, in build_doc
        encoding = get_encoding(page) or "utf-8"
                   ~~~~~~~~~~~~^^^^^^
      File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.13/site-packages/readability/encoding.py", line 34, in get_encoding
        RE_CHARSET.findall(page) + RE_PRAGMA.findall(page) + RE_XML.findall(page)
        ~~~~~~~~~~~~~~~~~~^^^^^^
    TypeError: cannot use a string pattern on a bytes-like object
    Traceback (most recent call last):
      File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.13/site-packages/readability/readability.py", line 227, in summary
        self._html(True)
        ~~~~~~~~~~^^^^^^
      File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.13/site-packages/readability/readability.py", line 153, in _html
        self.html = self._parse(self.input)
                    ~~~~~~~~~~~^^^^^^^^^^^^
      File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.13/site-packages/readability/readability.py", line 166, in _parse
        doc, self.encoding = build_doc(input)
                             ~~~~~~~~~^^^^^^^
      File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.13/site-packages/readability/htmls.py", line 16, in build_doc
        encoding = get_encoding(page) or "utf-8"
                   ~~~~~~~~~~~~^^^^^^
      File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.13/site-packages/readability/encoding.py", line 34, in get_encoding
        RE_CHARSET.findall(page) + RE_PRAGMA.findall(page) + RE_XML.findall(page)
        ~~~~~~~~~~~~~~~~~~^^^^^^
    TypeError: cannot use a string pattern on a bytes-like object
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
        start(fakepyfile,mainpyfile)    ~~~~~^^^^^^^^^^^^^^^^^^^^^^^  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
        exec(open(mainpyfile).read(),  __main__.__dict__)
        ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<string>", line 6, in <module>
      File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.13/site-packages/readability/readability.py", line 274, in summary
        raise Unparseable(str(e)).with_traceback(sys.exc_info()[2])
      File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.13/site-packages/readability/readability.py", line 227, in summary
        self._html(True)
        ~~~~~~~~~~^^^^^^
      File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.13/site-packages/readability/readability.py", line 153, in _html
        self.html = self._parse(self.input)
                    ~~~~~~~~~~~^^^^^^^^^^^^
      File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.13/site-packages/readability/readability.py", line 166, in _parse
        doc, self.encoding = build_doc(input)
                             ~~~~~~~~~^^^^^^^
      File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.13/site-packages/readability/htmls.py", line 16, in build_doc
        encoding = get_encoding(page) or "utf-8"
                   ~~~~~~~~~~~~^^^^^^
      File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.13/site-packages/readability/encoding.py", line 34, in get_encoding
        RE_CHARSET.findall(page) + RE_PRAGMA.findall(page) + RE_XML.findall(page)
        ~~~~~~~~~~~~~~~~~~^^^^^^
    readability.readability.Unparseable: cannot use a string pattern on a bytes-like object
    
    [Program finished]
  6. buriy commented on Aug 26, 2026

    @buriy
    Owner

    Fixed in 0.9. Document now accepts response bytes correctly, including the README requests example, with regression coverage for bytes input and encoding declarations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions