From 570303f9381c25de969ec6391d59b76fc8c933b2 Mon Sep 17 00:00:00 2001 From: Karim shoair Date: Tue, 16 Sep 2025 01:58:51 +0300 Subject: [PATCH] fix(parser): Make `html_content` and `prettify` return strings not bytes (depends on the encoding) --- scrapling/parser.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/scrapling/parser.py b/scrapling/parser.py index 409e09d..4fb510c 100644 --- a/scrapling/parser.py +++ b/scrapling/parser.py @@ -339,7 +339,10 @@ class Selector(SelectorsGeneration): @property def html_content(self) -> TextHandler: """Return the inner HTML code of the element""" - return TextHandler(tostring(self._root, encoding=self.encoding, method="html", with_tail=False)) + content = tostring(self._root, encoding=self.encoding, method="html", with_tail=False) + if isinstance(content, bytes): + content = content.decode("utf-8") + return TextHandler(content) @property def body(self): @@ -348,15 +351,16 @@ class Selector(SelectorsGeneration): def prettify(self) -> TextHandler: """Return a prettified version of the element's inner html-code""" - return TextHandler( - tostring( - self._root, - encoding=self.encoding, - pretty_print=True, - method="html", - with_tail=False, - ) + content = tostring( + self._root, + encoding=self.encoding, + pretty_print=True, + method="html", + with_tail=False, ) + if isinstance(content, bytes): + content = content.decode("utf-8") + return TextHandler(content) def has_class(self, class_name: str) -> bool: """Check if the element has a specific class