/
selector.py
854 lines (725 loc) · 27.6 KB
/
selector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
"""XPath and JMESPath selectors based on the lxml and jmespath Python
packages."""
import json
import typing
import warnings
from io import BytesIO
from typing import (
Any,
Dict,
List,
Literal,
Mapping,
Optional,
Pattern,
SupportsIndex,
Tuple,
Type,
TypedDict,
TypeVar,
Union,
)
from warnings import warn
import jmespath
from lxml import etree, html
from packaging.version import Version
from .csstranslator import GenericTranslator, HTMLTranslator
from .utils import extract_regex, flatten, iflatten, shorten
_SelectorType = TypeVar("_SelectorType", bound="Selector")
_ParserType = Union[etree.XMLParser, etree.HTMLParser]
# simplified _OutputMethodArg from types-lxml
_TostringMethodType = Literal[
"html",
"xml",
]
lxml_version = Version(etree.__version__)
lxml_huge_tree_version = Version("4.2")
LXML_SUPPORTS_HUGE_TREE = lxml_version >= lxml_huge_tree_version
class CannotRemoveElementWithoutRoot(Exception):
pass
class CannotRemoveElementWithoutParent(Exception):
pass
class CannotDropElementWithoutParent(CannotRemoveElementWithoutParent):
pass
class SafeXMLParser(etree.XMLParser): # type: ignore[type-arg]
def __init__(self, *args: Any, **kwargs: Any) -> None:
kwargs.setdefault("resolve_entities", False)
super().__init__(*args, **kwargs)
class CTGroupValue(TypedDict):
_parser: Union[Type[etree.XMLParser], Type[html.HTMLParser]] # type: ignore[type-arg]
_csstranslator: Union[GenericTranslator, HTMLTranslator]
_tostring_method: str
_ctgroup: Dict[str, CTGroupValue] = {
"html": {
"_parser": html.HTMLParser,
"_csstranslator": HTMLTranslator(),
"_tostring_method": "html",
},
"xml": {
"_parser": SafeXMLParser,
"_csstranslator": GenericTranslator(),
"_tostring_method": "xml",
},
}
def _xml_or_html(type: Optional[str]) -> str:
return "xml" if type == "xml" else "html"
def create_root_node(
text: str,
parser_cls: Type[_ParserType],
base_url: Optional[str] = None,
huge_tree: bool = LXML_SUPPORTS_HUGE_TREE,
body: bytes = b"",
encoding: str = "utf8",
) -> etree._Element:
"""Create root node for text using given parser class."""
if not text:
body = body.replace(b"\x00", b"").strip()
else:
body = text.strip().replace("\x00", "").encode(encoding) or b"<html/>"
if huge_tree and LXML_SUPPORTS_HUGE_TREE:
parser = parser_cls(recover=True, encoding=encoding, huge_tree=True)
root = etree.fromstring(body, parser=parser, base_url=base_url)
else:
parser = parser_cls(recover=True, encoding=encoding)
root = etree.fromstring(body, parser=parser, base_url=base_url)
for error in parser.error_log:
if "use XML_PARSE_HUGE option" in error.message:
warnings.warn(
f"Input data is too big. Upgrade to lxml "
f"{lxml_huge_tree_version} or later for huge_tree support."
)
if root is None:
root = etree.fromstring(b"<html/>", parser=parser, base_url=base_url)
return root
class SelectorList(List[_SelectorType]):
"""
The :class:`SelectorList` class is a subclass of the builtin ``list``
class, which provides a few additional methods.
"""
@typing.overload
def __getitem__(self, pos: "SupportsIndex") -> _SelectorType:
pass
@typing.overload
def __getitem__(self, pos: slice) -> "SelectorList[_SelectorType]":
pass
def __getitem__(
self, pos: Union["SupportsIndex", slice]
) -> Union[_SelectorType, "SelectorList[_SelectorType]"]:
o = super().__getitem__(pos)
if isinstance(pos, slice):
return self.__class__(typing.cast("SelectorList[_SelectorType]", o))
else:
return typing.cast(_SelectorType, o)
def __getstate__(self) -> None:
raise TypeError("can't pickle SelectorList objects")
def jmespath(self, query: str, **kwargs: Any) -> "SelectorList[_SelectorType]":
"""
Call the ``.jmespath()`` method for each element in this list and return
their results flattened as another :class:`SelectorList`.
``query`` is the same argument as the one in :meth:`Selector.jmespath`.
Any additional named arguments are passed to the underlying
``jmespath.search`` call, e.g.::
selector.jmespath('author.name', options=jmespath.Options(dict_cls=collections.OrderedDict))
"""
return self.__class__(flatten([x.jmespath(query, **kwargs) for x in self]))
def xpath(
self,
xpath: str,
namespaces: Optional[Mapping[str, str]] = None,
**kwargs: Any,
) -> "SelectorList[_SelectorType]":
"""
Call the ``.xpath()`` method for each element in this list and return
their results flattened as another :class:`SelectorList`.
``xpath`` is the same argument as the one in :meth:`Selector.xpath`
``namespaces`` is an optional ``prefix: namespace-uri`` mapping (dict)
for additional prefixes to those registered with ``register_namespace(prefix, uri)``.
Contrary to ``register_namespace()``, these prefixes are not
saved for future calls.
Any additional named arguments can be used to pass values for XPath
variables in the XPath expression, e.g.::
selector.xpath('//a[href=$url]', url="http://www.example.com")
"""
return self.__class__(
flatten([x.xpath(xpath, namespaces=namespaces, **kwargs) for x in self])
)
def css(self, query: str) -> "SelectorList[_SelectorType]":
"""
Call the ``.css()`` method for each element in this list and return
their results flattened as another :class:`SelectorList`.
``query`` is the same argument as the one in :meth:`Selector.css`
"""
return self.__class__(flatten([x.css(query) for x in self]))
def re(
self, regex: Union[str, Pattern[str]], replace_entities: bool = True
) -> List[str]:
"""
Call the ``.re()`` method for each element in this list and return
their results flattened, as a list of strings.
By default, character entity references are replaced by their
corresponding character (except for ``&`` and ``<``.
Passing ``replace_entities`` as ``False`` switches off these
replacements.
"""
return flatten([x.re(regex, replace_entities=replace_entities) for x in self])
@typing.overload
def re_first(
self,
regex: Union[str, Pattern[str]],
default: None = None,
replace_entities: bool = True,
) -> Optional[str]:
pass
@typing.overload
def re_first(
self,
regex: Union[str, Pattern[str]],
default: str,
replace_entities: bool = True,
) -> str:
pass
def re_first(
self,
regex: Union[str, Pattern[str]],
default: Optional[str] = None,
replace_entities: bool = True,
) -> Optional[str]:
"""
Call the ``.re()`` method for the first element in this list and
return the result in an string. If the list is empty or the
regex doesn't match anything, return the default value (``None`` if
the argument is not provided).
By default, character entity references are replaced by their
corresponding character (except for ``&`` and ``<``.
Passing ``replace_entities`` as ``False`` switches off these
replacements.
"""
for el in iflatten(
x.re(regex, replace_entities=replace_entities) for x in self
):
return typing.cast(str, el)
return default
def getall(self) -> List[str]:
"""
Call the ``.get()`` method for each element is this list and return
their results flattened, as a list of strings.
"""
return [x.get() for x in self]
extract = getall
@typing.overload
def get(self, default: None = None) -> Optional[str]:
pass
@typing.overload
def get(self, default: str) -> str:
pass
def get(self, default: Optional[str] = None) -> Any:
"""
Return the result of ``.get()`` for the first element in this list.
If the list is empty, return the default value.
"""
for x in self:
return x.get()
return default
extract_first = get
@property
def attrib(self) -> Mapping[str, str]:
"""Return the attributes dictionary for the first element.
If the list is empty, return an empty dict.
"""
for x in self:
return x.attrib
return {}
def remove(self) -> None: # type: ignore[override]
"""
Remove matched nodes from the parent for each element in this list.
"""
warn(
"Method parsel.selector.SelectorList.remove is deprecated, please use parsel.selector.SelectorList.drop method instead",
category=DeprecationWarning,
stacklevel=2,
)
for x in self:
x.remove()
def drop(self) -> None:
"""
Drop matched nodes from the parent for each element in this list.
"""
for x in self:
x.drop()
_NOT_SET = object()
def _get_root_from_text(text: str, *, type: str, **lxml_kwargs: Any) -> etree._Element:
return create_root_node(text, _ctgroup[type]["_parser"], **lxml_kwargs)
def _get_root_and_type_from_bytes(
body: bytes,
encoding: str,
*,
input_type: Optional[str],
**lxml_kwargs: Any,
) -> Tuple[Any, str]:
if input_type == "text":
return body.decode(encoding), input_type
if encoding == "utf8":
try:
data = json.load(BytesIO(body))
except ValueError:
data = _NOT_SET
if data is not _NOT_SET:
return data, "json"
if input_type == "json":
return None, "json"
assert input_type in ("html", "xml", None) # nosec
type = _xml_or_html(input_type)
root = create_root_node(
text="",
body=body,
encoding=encoding,
parser_cls=_ctgroup[type]["_parser"],
**lxml_kwargs,
)
return root, type
def _get_root_and_type_from_text(
text: str, *, input_type: Optional[str], **lxml_kwargs: Any
) -> Tuple[Any, str]:
if input_type == "text":
return text, input_type
try:
data = json.loads(text)
except ValueError:
data = _NOT_SET
if data is not _NOT_SET:
return data, "json"
if input_type == "json":
return None, "json"
assert input_type in ("html", "xml", None) # nosec
type = _xml_or_html(input_type)
root = _get_root_from_text(text, type=type, **lxml_kwargs)
return root, type
def _get_root_type(root: Any, *, input_type: Optional[str]) -> str:
if isinstance(root, etree._Element): # pylint: disable=protected-access
if input_type in {"json", "text"}:
raise ValueError(
f"Selector got an lxml.etree._Element object as root, "
f"and {input_type!r} as type."
)
return _xml_or_html(input_type)
elif isinstance(root, (dict, list)) or _is_valid_json(root):
return "json"
return input_type or "json"
def _is_valid_json(text: str) -> bool:
try:
json.loads(text)
except (TypeError, ValueError):
return False
else:
return True
def _load_json_or_none(text: str) -> Any:
if isinstance(text, (str, bytes, bytearray)):
try:
return json.loads(text)
except ValueError:
return None
return None
class Selector:
"""Wrapper for input data in HTML, JSON, or XML format, that allows
selecting parts of it using selection expressions.
You can write selection expressions in CSS or XPath for HTML and XML
inputs, or in JMESPath for JSON inputs.
``text`` is an ``str`` object.
``body`` is a ``bytes`` object. It can be used together with the
``encoding`` argument instead of the ``text`` argument.
``type`` defines the selector type. It can be ``"html"`` (default),
``"json"``, or ``"xml"``.
``base_url`` allows setting a URL for the document. This is needed when looking up external entities with relative paths.
See the documentation for :func:`lxml.etree.fromstring` for more information.
``huge_tree`` controls the lxml/libxml2 feature that forbids parsing
certain large documents to protect from possible memory exhaustion. The
argument is ``True`` by default if the installed lxml version supports it,
which disables the protection to allow parsing such documents. Set it to
``False`` if you want to enable the protection.
See `this lxml FAQ entry <https://lxml.de/FAQ.html#is-lxml-vulnerable-to-xml-bombs>`_
for more information.
"""
__slots__ = [
"namespaces",
"type",
"_expr",
"_huge_tree",
"root",
"_text",
"body",
"__weakref__",
]
_default_namespaces = {
"re": "http://exslt.org/regular-expressions",
# supported in libxslt:
# set:difference
# set:has-same-node
# set:intersection
# set:leading
# set:trailing
"set": "http://exslt.org/sets",
}
_lxml_smart_strings = False
selectorlist_cls = SelectorList["Selector"]
def __init__(
self,
text: Optional[str] = None,
type: Optional[str] = None,
body: bytes = b"",
encoding: str = "utf8",
namespaces: Optional[Mapping[str, str]] = None,
root: Optional[Any] = _NOT_SET,
base_url: Optional[str] = None,
_expr: Optional[str] = None,
huge_tree: bool = LXML_SUPPORTS_HUGE_TREE,
) -> None:
self.root: Any
if type not in ("html", "json", "text", "xml", None):
raise ValueError(f"Invalid type: {type}")
if text is None and not body and root is _NOT_SET:
raise ValueError("Selector needs text, body, or root arguments")
if text is not None and not isinstance(text, str):
msg = f"text argument should be of type str, got {text.__class__}"
raise TypeError(msg)
if text is not None:
if root is not _NOT_SET:
warnings.warn(
"Selector got both text and root, root is being ignored.",
stacklevel=2,
)
if not isinstance(text, str):
msg = f"text argument should be of type str, got {text.__class__}"
raise TypeError(msg)
root, type = _get_root_and_type_from_text(
text,
input_type=type,
base_url=base_url,
huge_tree=huge_tree,
)
self.root = root
self.type = type
elif body:
if not isinstance(body, bytes):
msg = f"body argument should be of type bytes, got {body.__class__}"
raise TypeError(msg)
root, type = _get_root_and_type_from_bytes(
body=body,
encoding=encoding,
input_type=type,
base_url=base_url,
huge_tree=huge_tree,
)
self.root = root
self.type = type
elif root is _NOT_SET:
raise ValueError("Selector needs text, body, or root arguments")
else:
self.root = root
self.type = _get_root_type(root, input_type=type)
self.namespaces = dict(self._default_namespaces)
if namespaces is not None:
self.namespaces.update(namespaces)
self._expr = _expr
self._huge_tree = huge_tree
self._text = text
def __getstate__(self) -> Any:
raise TypeError("can't pickle Selector objects")
def _get_root(
self,
text: str = "",
base_url: Optional[str] = None,
huge_tree: bool = LXML_SUPPORTS_HUGE_TREE,
type: Optional[str] = None,
body: bytes = b"",
encoding: str = "utf8",
) -> etree._Element:
return create_root_node(
text,
body=body,
encoding=encoding,
parser_cls=_ctgroup[type or self.type]["_parser"],
base_url=base_url,
huge_tree=huge_tree,
)
def jmespath(
self: _SelectorType,
query: str,
**kwargs: Any,
) -> SelectorList[_SelectorType]:
"""
Find objects matching the JMESPath ``query`` and return the result as a
:class:`SelectorList` instance with all elements flattened. List
elements implement :class:`Selector` interface too.
``query`` is a string containing the `JMESPath
<https://jmespath.org/>`_ query to apply.
Any additional named arguments are passed to the underlying
``jmespath.search`` call, e.g.::
selector.jmespath('author.name', options=jmespath.Options(dict_cls=collections.OrderedDict))
"""
if self.type == "json":
if isinstance(self.root, str):
# Selector received a JSON string as root.
data = _load_json_or_none(self.root)
else:
data = self.root
else:
assert self.type in {"html", "xml"} # nosec
data = _load_json_or_none(self.root.text)
result = jmespath.search(query, data, **kwargs)
if result is None:
result = []
elif not isinstance(result, list):
result = [result]
def make_selector(x: Any) -> _SelectorType: # closure function
if isinstance(x, str):
return self.__class__(text=x, _expr=query, type="text")
else:
return self.__class__(root=x, _expr=query)
result = [make_selector(x) for x in result]
return typing.cast(SelectorList[_SelectorType], self.selectorlist_cls(result))
def xpath(
self: _SelectorType,
query: str,
namespaces: Optional[Mapping[str, str]] = None,
**kwargs: Any,
) -> SelectorList[_SelectorType]:
"""
Find nodes matching the xpath ``query`` and return the result as a
:class:`SelectorList` instance with all elements flattened. List
elements implement :class:`Selector` interface too.
``query`` is a string containing the XPATH query to apply.
``namespaces`` is an optional ``prefix: namespace-uri`` mapping (dict)
for additional prefixes to those registered with ``register_namespace(prefix, uri)``.
Contrary to ``register_namespace()``, these prefixes are not
saved for future calls.
Any additional named arguments can be used to pass values for XPath
variables in the XPath expression, e.g.::
selector.xpath('//a[href=$url]', url="http://www.example.com")
"""
if self.type not in ("html", "xml", "text"):
raise ValueError(f"Cannot use xpath on a Selector of type {self.type!r}")
if self.type in ("html", "xml"):
try:
xpathev = self.root.xpath
except AttributeError:
return typing.cast(
SelectorList[_SelectorType], self.selectorlist_cls([])
)
else:
try:
xpathev = self._get_root(self._text or "", type="html").xpath
except AttributeError:
return typing.cast(
SelectorList[_SelectorType], self.selectorlist_cls([])
)
nsp = dict(self.namespaces)
if namespaces is not None:
nsp.update(namespaces)
try:
result = xpathev(
query,
namespaces=nsp,
smart_strings=self._lxml_smart_strings,
**kwargs,
)
except etree.XPathError as exc:
raise ValueError(f"XPath error: {exc} in {query}")
if type(result) is not list:
result = [result]
result = [
self.__class__(
root=x,
_expr=query,
namespaces=self.namespaces,
type=_xml_or_html(self.type),
)
for x in result
]
return typing.cast(SelectorList[_SelectorType], self.selectorlist_cls(result))
def css(self: _SelectorType, query: str) -> SelectorList[_SelectorType]:
"""
Apply the given CSS selector and return a :class:`SelectorList` instance.
``query`` is a string containing the CSS selector to apply.
In the background, CSS queries are translated into XPath queries using
`cssselect`_ library and run ``.xpath()`` method.
.. _cssselect: https://pypi.python.org/pypi/cssselect/
"""
if self.type not in ("html", "xml", "text"):
raise ValueError(f"Cannot use css on a Selector of type {self.type!r}")
return self.xpath(self._css2xpath(query))
def _css2xpath(self, query: str) -> str:
type = _xml_or_html(self.type)
return _ctgroup[type]["_csstranslator"].css_to_xpath(query)
def re(
self, regex: Union[str, Pattern[str]], replace_entities: bool = True
) -> List[str]:
"""
Apply the given regex and return a list of strings with the
matches.
``regex`` can be either a compiled regular expression or a string which
will be compiled to a regular expression using ``re.compile(regex)``.
By default, character entity references are replaced by their
corresponding character (except for ``&`` and ``<``).
Passing ``replace_entities`` as ``False`` switches off these
replacements.
"""
data = self.get()
return extract_regex(regex, data, replace_entities=replace_entities)
@typing.overload
def re_first(
self,
regex: Union[str, Pattern[str]],
default: None = None,
replace_entities: bool = True,
) -> Optional[str]:
pass
@typing.overload
def re_first(
self,
regex: Union[str, Pattern[str]],
default: str,
replace_entities: bool = True,
) -> str:
pass
def re_first(
self,
regex: Union[str, Pattern[str]],
default: Optional[str] = None,
replace_entities: bool = True,
) -> Optional[str]:
"""
Apply the given regex and return the first string which matches. If
there is no match, return the default value (``None`` if the argument
is not provided).
By default, character entity references are replaced by their
corresponding character (except for ``&`` and ``<``).
Passing ``replace_entities`` as ``False`` switches off these
replacements.
"""
return next(
iflatten(self.re(regex, replace_entities=replace_entities)),
default,
)
def get(self) -> Any:
"""
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
try:
return typing.cast(
str,
etree.tostring(
self.root,
method=_ctgroup[self.type]["_tostring_method"],
encoding="unicode",
with_tail=False,
),
)
except (AttributeError, TypeError):
if self.root is True:
return "1"
elif self.root is False:
return "0"
else:
return str(self.root)
extract = get
def getall(self) -> List[str]:
"""
Serialize and return the matched node in a 1-element list of strings.
"""
return [self.get()]
def register_namespace(self, prefix: str, uri: str) -> None:
"""
Register the given namespace to be used in this :class:`Selector`.
Without registering namespaces you can't select or extract data from
non-standard namespaces. See :ref:`selector-examples-xml`.
"""
self.namespaces[prefix] = uri
def remove_namespaces(self) -> None:
"""
Remove all namespaces, allowing to traverse the document using
namespace-less xpaths. See :ref:`removing-namespaces`.
"""
for el in self.root.iter("*"):
if el.tag.startswith("{"):
el.tag = el.tag.split("}", 1)[1]
# loop on element attributes also
for an in el.attrib:
if an.startswith("{"):
el.attrib[an.split("}", 1)[1]] = el.attrib.pop(an)
# remove namespace declarations
etree.cleanup_namespaces(self.root)
def remove(self) -> None:
"""
Remove matched nodes from the parent element.
"""
warn(
"Method parsel.selector.Selector.remove is deprecated, please use parsel.selector.Selector.drop method instead",
category=DeprecationWarning,
stacklevel=2,
)
try:
parent = self.root.getparent()
except AttributeError:
# 'str' object has no attribute 'getparent'
raise CannotRemoveElementWithoutRoot(
"The node you're trying to remove has no root, "
"are you trying to remove a pseudo-element? "
"Try to use 'li' as a selector instead of 'li::text' or "
"'//li' instead of '//li/text()', for example."
)
try:
parent.remove(self.root)
except AttributeError:
# 'NoneType' object has no attribute 'remove'
raise CannotRemoveElementWithoutParent(
"The node you're trying to remove has no parent, "
"are you trying to remove a root element?"
)
def drop(self) -> None:
"""
Drop matched nodes from the parent element.
"""
try:
parent = self.root.getparent()
except AttributeError:
# 'str' object has no attribute 'getparent'
raise CannotRemoveElementWithoutRoot(
"The node you're trying to drop has no root, "
"are you trying to drop a pseudo-element? "
"Try to use 'li' as a selector instead of 'li::text' or "
"'//li' instead of '//li/text()', for example."
)
try:
if self.type == "xml":
if parent is None:
raise ValueError("This node has no parent")
parent.remove(self.root)
else:
typing.cast(html.HtmlElement, self.root).drop_tree()
except (AttributeError, AssertionError):
# 'NoneType' object has no attribute 'drop'
raise CannotDropElementWithoutParent(
"The node you're trying to remove has no parent, "
"are you trying to remove a root element?"
)
@property
def attrib(self) -> Dict[str, str]:
"""Return the attributes dictionary for underlying element."""
return dict(self.root.attrib)
def __bool__(self) -> bool:
"""
Return ``True`` if there is any real content selected or ``False``
otherwise. In other words, the boolean value of a :class:`Selector` is
given by the contents it selects.
"""
return bool(self.get())
__nonzero__ = __bool__
def __str__(self) -> str:
return str(self.get())
def __repr__(self) -> str:
data = repr(shorten(str(self.get()), width=40))
return f"<{type(self).__name__} query={self._expr!r} data={data}>"