diff --git a/parsel/selector.py b/parsel/selector.py index 8d884a4..8717b40 100644 --- a/parsel/selector.py +++ b/parsel/selector.py @@ -745,8 +745,10 @@ def re_first( def get(self) -> Any: """ - Serialize and return the matched nodes in a single string. - Percent encoded content is unquoted. + Serialize and return the matched nodes. + + For HTML and XML, the result is always a string, and percent-encoded + content is unquoted. """ if self.type in ("text", "json"): return self.root @@ -873,7 +875,8 @@ def __bool__(self) -> bool: __nonzero__ = __bool__ def __str__(self) -> str: - data = repr(shorten(self.get(), width=40)) - return f"<{type(self).__name__} query={self._expr!r} data={data}>" + return str(self.get()) - __repr__ = __str__ + def __repr__(self) -> str: + data = repr(shorten(str(self.get()), width=40)) + return f"<{type(self).__name__} query={self._expr!r} data={data}>" diff --git a/tests/test_selector.py b/tests/test_selector.py index c96ecf5..62867c6 100644 --- a/tests/test_selector.py +++ b/tests/test_selector.py @@ -1252,6 +1252,14 @@ def test_etree_root_invalid_type(self) -> None: type="json", ) + def test_json_selector_representation(self) -> None: + selector = Selector(text="true") + assert repr(selector) == "" + assert str(selector) == "True" + selector = Selector(text="1") + assert repr(selector) == "" + assert str(selector) == "1" + class ExsltTestCase(unittest.TestCase):