From ce4fc31f2c30a322ba1bbd05bf43c3ba845f91b0 Mon Sep 17 00:00:00 2001 From: Karim shoair Date: Sun, 14 Sep 2025 20:21:45 +0300 Subject: [PATCH] feat: Make `.body` return the passed content as it is without any processing This makes it possible to download files and deal with non-HTML requests (ex: #81 ) --- scrapling/parser.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/scrapling/parser.py b/scrapling/parser.py index 07576b0..409e09d 100644 --- a/scrapling/parser.py +++ b/scrapling/parser.py @@ -132,8 +132,7 @@ class Selector(SelectorsGeneration): strip_cdata=(not keep_cdata), ) self._root = fromstring(body, parser=parser, base_url=url) - - self._raw_body = body.decode() + self._raw_body = content else: # All HTML types inherit from HtmlMixin so this to check for all at once @@ -342,7 +341,10 @@ class Selector(SelectorsGeneration): """Return the inner HTML code of the element""" return TextHandler(tostring(self._root, encoding=self.encoding, method="html", with_tail=False)) - body = html_content + @property + def body(self): + """Return the raw body of the current `Selector` without any processing. Useful for binary and non-HTML requests.""" + return self._raw_body def prettify(self) -> TextHandler: """Return a prettified version of the element's inner html-code""" @@ -934,7 +936,7 @@ class Selector(SelectorsGeneration): # Operations on text functions def json(self) -> Dict: """Return JSON response if the response is jsonable otherwise throws error""" - if self._raw_body: + if self._raw_body and isinstance(self._raw_body, str): return TextHandler(self._raw_body).json() elif self.text: return self.text.json()