Skip to content

🕷️ The BEST Web Scraping framework that handles everything from a single request to a full-scale crawl! Don't be shy, join here: https://discord.gg/EMgGbDceNQ and follow here for daily tips and tricks: https://x.com/Scrapling_dev

fix(parser): Make `html_content` and `prettify` return strings not bytes (depends on the encoding)

Karim shoair570303f9381c
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