From 0478809ea1e8ae324e324e99cbb2e25bd0bf31b3 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Mon, 20 Nov 2023 20:49:22 +0300 Subject: [PATCH] Fix(Py3): `html.parser` cannot parse bytes Decode to unicode from a known encoding. --- parse_html/bkmk_ph_htmlparser.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/parse_html/bkmk_ph_htmlparser.py b/parse_html/bkmk_ph_htmlparser.py index c0f89b4..d11a2ff 100644 --- a/parse_html/bkmk_ph_htmlparser.py +++ b/parse_html/bkmk_ph_htmlparser.py @@ -91,6 +91,11 @@ class HTMLParser(_HTMLParser): def parse_html(html_text, charset=None, log=None): if not html_text: return None + if charset is None and isinstance(html_text, bytes): + return None # html.parser cannot parse bytes + if charset and isinstance(html_text, bytes): + html_text = html_text.decode(charset) + parser = HTMLParser(charset) try: -- 2.39.2