selectolax 0.3.29__cp39-cp39-musllinux_1_2_aarch64.whl → 0.4.0__cp39-cp39-musllinux_1_2_aarch64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of selectolax might be problematic. Click here for more details.

selectolax/lexbor.pyi CHANGED
@@ -1,8 +1,10 @@
1
- from typing import Any, Iterator, Literal, TypeVar, NoReturn, overload
1
+ from typing import Any, Iterator, Literal, NoReturn, Optional, TypeVar, overload
2
2
 
3
3
  DefaultT = TypeVar("DefaultT")
4
4
 
5
5
  class LexborAttributes:
6
+ """A dict-like object that represents attributes."""
7
+
6
8
  @staticmethod
7
9
  def create(node: LexborAttributes) -> LexborAttributes: ...
8
10
  def keys(self) -> Iterator[str]: ...
@@ -11,7 +13,7 @@ class LexborAttributes:
11
13
  def __iter__(self) -> Iterator[str]: ...
12
14
  def __len__(self) -> int: ...
13
15
  def __getitem__(self, key: str) -> str | None: ...
14
- def __setitem__(self, key: str, value: str) -> None: ...
16
+ def __setitem__(self, key: str, value: Optional[str]) -> None: ...
15
17
  def __delitem__(self, key: str) -> None: ...
16
18
  def __contains__(self, key: str) -> bool: ...
17
19
  def __repr__(self) -> str: ...
@@ -25,24 +27,77 @@ class LexborAttributes:
25
27
  def sget(self, key: str, default: str = "") -> str: ...
26
28
 
27
29
  class LexborSelector:
30
+ """An advanced CSS selector that supports additional operations.
31
+
32
+ Think of it as a toolkit that mimics some of the features of XPath.
33
+
34
+ Please note, this is an experimental feature that can change in the future.
35
+ """
36
+
28
37
  def __init__(self, node: LexborNode, query: str): ...
29
38
  def css(self, query: str) -> NoReturn: ...
30
39
  @property
31
- def matches(self) -> list[LexborNode]: ...
40
+ def matches(self) -> list[LexborNode]:
41
+ """Returns all possible matches"""
42
+ ...
32
43
  @property
33
- def any_matches(self) -> bool: ...
44
+ def any_matches(self) -> bool:
45
+ """Returns True if there are any matches"""
46
+ ...
34
47
  def text_contains(
35
48
  self, text: str, deep: bool = True, separator: str = "", strip: bool = False
36
- ) -> LexborSelector: ...
49
+ ) -> LexborSelector:
50
+ """Filter all current matches given text."""
51
+ ...
37
52
  def any_text_contains(
38
53
  self, text: str, deep: bool = True, separator: str = "", strip: bool = False
39
- ) -> bool: ...
54
+ ) -> bool:
55
+ """Returns True if any node in the current search scope contains specified text"""
56
+ ...
40
57
  def attribute_longer_than(
41
58
  self, attribute: str, length: int, start: str | None = None
42
- ) -> LexborSelector: ...
59
+ ) -> LexborSelector:
60
+ """Filter all current matches by attribute length.
61
+
62
+ Similar to string-length in XPath.
63
+ """
64
+ ...
43
65
  def any_attribute_longer_than(
44
66
  self, attribute: str, length: int, start: str | None = None
45
- ) -> bool: ...
67
+ ) -> bool:
68
+ """Returns True any href attribute longer than a specified length.
69
+
70
+ Similar to string-length in XPath.
71
+ """
72
+ ...
73
+
74
+ @property
75
+ def inner_html(self) -> str | None:
76
+ """Return HTML representation of the child nodes.
77
+
78
+ Works similar to innerHTML in JavaScript.
79
+ Unlike the `.html` property, does not include the current node.
80
+ Can be used to set HTML as well. See the setter docstring.
81
+
82
+ Returns
83
+ -------
84
+ text : str or None
85
+ """
86
+ ...
87
+
88
+ @inner_html.setter
89
+ def inner_html(self, html: str):
90
+ """Set inner HTML to the specified HTML.
91
+
92
+ Replaces existing data inside the node.
93
+ Works similar to innerHTML in JavaScript.
94
+
95
+ Parameters
96
+ ----------
97
+ html : str
98
+
99
+ """
100
+ ...
46
101
 
47
102
  class LexborCSSSelector:
48
103
  def __init__(self): ...
@@ -50,109 +105,831 @@ class LexborCSSSelector:
50
105
  def any_matches(self, query: str, node: LexborNode) -> bool: ...
51
106
 
52
107
  class LexborNode:
108
+ """A class that represents HTML node (element)."""
109
+
53
110
  parser: LexborHTMLParser
54
111
  @property
55
112
  def mem_id(self) -> int: ...
56
113
  @property
57
- def child(self) -> LexborNode | None: ...
114
+ def child(self) -> LexborNode | None:
115
+ """Alias for the `first_child` property.
116
+
117
+ **Deprecated**. Please use `first_child` instead.
118
+ """
119
+ ...
58
120
  @property
59
- def first_child(self) -> LexborNode | None: ...
121
+ def first_child(self) -> LexborNode | None:
122
+ """Return the first child node."""
123
+ ...
60
124
  @property
61
- def parent(self) -> LexborNode | None: ...
125
+ def parent(self) -> LexborNode | None:
126
+ """Return the parent node."""
127
+ ...
62
128
  @property
63
- def next(self) -> LexborNode | None: ...
129
+ def next(self) -> LexborNode | None:
130
+ """Return next node."""
131
+ ...
64
132
  @property
65
- def prev(self) -> LexborNode | None: ...
133
+ def prev(self) -> LexborNode | None:
134
+ """Return previous node."""
135
+ ...
66
136
  @property
67
- def last_child(self) -> LexborNode | None: ...
137
+ def last_child(self) -> LexborNode | None:
138
+ """Return last child node."""
139
+ ...
68
140
  @property
69
- def html(self) -> str | None: ...
141
+ def html(self) -> str | None:
142
+ """Return HTML representation of the current node including all its child nodes.
143
+
144
+ Returns
145
+ -------
146
+ text : str
147
+ """
148
+ ...
70
149
  def __hash__(self) -> int: ...
71
- def text_lexbor(self) -> str: ...
72
- def text(
73
- self, deep: bool = True, separator: str = "", strip: bool = False
74
- ) -> str: ...
75
- def css(self, query: str) -> list[LexborNode]: ...
150
+ def text_lexbor(self) -> str:
151
+ """Returns the text of the node including text of all its child nodes.
152
+
153
+ Uses builtin method from lexbor.
154
+ """
155
+ ...
156
+ def text(self, deep: bool = True, separator: str = "", strip: bool = False) -> str:
157
+ """Returns the text of the node including text of all its child nodes.
158
+
159
+ Parameters
160
+ ----------
161
+ strip : bool, default False
162
+ If true, calls ``str.strip()`` on each text part to remove extra white spaces.
163
+ separator : str, default ''
164
+ The separator to use when joining text from different nodes.
165
+ deep : bool, default True
166
+ If True, includes text from all child nodes.
167
+
168
+ Returns
169
+ -------
170
+ text : str
171
+ """
172
+ ...
173
+ def css(self, query: str) -> list[LexborNode]:
174
+ """Evaluate CSS selector against current node and its child nodes.
175
+
176
+ Matches pattern `query` against HTML tree.
177
+ `CSS selectors reference <https://www.w3schools.com/cssref/css_selectors.asp>`_.
178
+
179
+ Special selectors:
180
+
181
+ - parser.css('p:lexbor-contains("awesome" i)') -- case-insensitive contains
182
+ - parser.css('p:lexbor-contains("awesome")') -- case-sensitive contains
183
+
184
+
185
+ Parameters
186
+ ----------
187
+ query : str
188
+ CSS selector (e.g. "div > :nth-child(2n+1):not(:has(a))").
189
+
190
+ Returns
191
+ -------
192
+ selector : list of `Node` objects
193
+ """
194
+ ...
76
195
  @overload
77
196
  def css_first(
78
197
  self, query: str, default: Any = ..., strict: Literal[True] = ...
79
- ) -> LexborNode: ...
198
+ ) -> LexborNode:
199
+ """Same as `css` but returns only the first match.
200
+
201
+ Parameters
202
+ ----------
203
+
204
+ query : str
205
+ default : bool, default None
206
+ Default value to return if there is no match.
207
+ strict: bool, default False
208
+ Set to True if you want to check if there is strictly only one match in the document.
209
+
210
+
211
+ Returns
212
+ -------
213
+ selector : `LexborNode` object
214
+ """
215
+ ...
80
216
  @overload
81
217
  def css_first(
82
218
  self, query: str, default: DefaultT, strict: bool = False
83
- ) -> LexborNode | DefaultT: ...
219
+ ) -> LexborNode | DefaultT:
220
+ """Same as `css` but returns only the first match.
221
+
222
+ Parameters
223
+ ----------
224
+
225
+ query : str
226
+ default : bool, default None
227
+ Default value to return if there is no match.
228
+ strict: bool, default False
229
+ Set to True if you want to check if there is strictly only one match in the document.
230
+
231
+
232
+ Returns
233
+ -------
234
+ selector : `LexborNode` object
235
+ """
236
+ ...
84
237
  @overload
85
238
  def css_first(
86
239
  self, query: str, default: None = ..., strict: bool = False
87
- ) -> LexborNode | None: ...
88
- def any_css_matches(self, selectors: tuple[str]) -> bool: ...
89
- def css_matches(self, selector: str) -> bool: ...
240
+ ) -> LexborNode | None:
241
+ """Same as `css` but returns only the first match.
242
+
243
+ Parameters
244
+ ----------
245
+
246
+ query : str
247
+ default : bool, default None
248
+ Default value to return if there is no match.
249
+ strict: bool, default False
250
+ Set to True if you want to check if there is strictly only one match in the document.
251
+
252
+
253
+ Returns
254
+ -------
255
+ selector : `LexborNode` object
256
+ """
257
+ ...
258
+ def any_css_matches(self, selectors: tuple[str]) -> bool:
259
+ """Returns True if any of CSS selectors matches a node"""
260
+ ...
261
+ def css_matches(self, selector: str) -> bool:
262
+ """Returns True if CSS selector matches a node."""
263
+ ...
90
264
  @property
91
265
  def tag_id(self) -> int: ...
92
266
  @property
93
- def tag(self) -> str | None: ...
94
- def decompose(self, recursive: bool = True) -> None: ...
95
- def strip_tags(self, tags: list[str], recursive: bool = False) -> None: ...
267
+ def tag(self) -> str | None:
268
+ """Return the name of the current tag (e.g. div, p, img).
269
+
270
+ For for non-tag nodes, returns the following names:
271
+
272
+ * `-text` - text node
273
+ * `-document` - document node
274
+ * `-comment` - comment node
275
+
276
+ Returns
277
+ -------
278
+ text : str
279
+ """
280
+ ...
281
+ def decompose(self, recursive: bool = True) -> None:
282
+ """Remove the current node from the tree.
283
+
284
+ Parameters
285
+ ----------
286
+ recursive : bool, default True
287
+ Whenever to delete all its child nodes
288
+
289
+ Examples
290
+ --------
291
+
292
+ >>> tree = LexborHTMLParser(html)
293
+ >>> for tag in tree.css('script'):
294
+ >>> tag.decompose()
295
+ """
296
+ ...
297
+ def strip_tags(self, tags: list[str], recursive: bool = False) -> None:
298
+ """Remove specified tags from the HTML tree.
299
+
300
+ Parameters
301
+ ----------
302
+ tags : list
303
+ List of tags to remove.
304
+ recursive : bool, default True
305
+ Whenever to delete all its child nodes
306
+
307
+ Examples
308
+ --------
309
+
310
+ >>> tree = LexborHTMLParser('<html><head></head><body><script></script><div>Hello world!</div></body></html>')
311
+ >>> tags = ['head', 'style', 'script', 'xmp', 'iframe', 'noembed', 'noframes']
312
+ >>> tree.strip_tags(tags)
313
+ >>> tree.html
314
+ '<html><body><div>Hello world!</div></body></html>'
315
+ """
316
+ ...
96
317
  @property
97
- def attributes(self) -> dict[str, str | None]: ...
318
+ def attributes(self) -> dict[str, str | None]:
319
+ """Get all attributes that belong to the current node.
320
+
321
+ The value of empty attributes is None.
322
+
323
+ Returns
324
+ -------
325
+ attributes : dictionary of all attributes.
326
+
327
+ Examples
328
+ --------
329
+
330
+ >>> tree = LexborHTMLParser("<div data id='my_id'></div>")
331
+ >>> node = tree.css_first('div')
332
+ >>> node.attributes
333
+ {'data': None, 'id': 'my_id'}
334
+ """
335
+ ...
98
336
  @property
99
- def attrs(self) -> LexborAttributes: ...
337
+ def attrs(self) -> LexborAttributes:
338
+ """A dict-like object that is similar to the ``attributes`` property, but operates directly on the Node data.
339
+
340
+ .. warning:: Use ``attributes`` instead, if you don't want to modify Node attributes.
341
+
342
+ Returns
343
+ -------
344
+ attributes : Attributes mapping object.
345
+
346
+ Examples
347
+ --------
348
+
349
+ >>> tree = LexborHTMLParser("<div id='a'></div>")
350
+ >>> node = tree.css_first('div')
351
+ >>> node.attrs
352
+ <div attributes, 1 items>
353
+ >>> node.attrs['id']
354
+ 'a'
355
+ >>> node.attrs['foo'] = 'bar'
356
+ >>> del node.attrs['id']
357
+ >>> node.attributes
358
+ {'foo': 'bar'}
359
+ >>> node.attrs['id'] = 'new_id'
360
+ >>> node.html
361
+ '<div foo="bar" id="new_id"></div>'
362
+ """
363
+ ...
100
364
  @property
101
- def id(self) -> str | None: ...
102
- def iter(self, include_text: bool = False) -> Iterator[LexborNode]: ...
103
- def unwrap(self) -> None: ...
104
- def unwrap_tags(self, tags: list[str], delete_empty : bool = False) -> None: ...
105
- def traverse(self, include_text: bool = False) -> Iterator[LexborNode]: ...
106
- def replace_with(self, value: bytes | str | LexborNode) -> None: ...
107
- def insert_before(self, value: bytes | str | LexborNode) -> None: ...
108
- def insert_after(self, value: bytes | str | LexborNode) -> None: ...
109
- def insert_child(self, value: bytes | str | LexborNode) -> None: ...
365
+ def id(self) -> str | None:
366
+ """Get the id attribute of the node.
367
+
368
+ Returns None if id does not set.
369
+
370
+ Returns
371
+ -------
372
+ text : str
373
+ """
374
+ ...
375
+ def iter(self, include_text: bool = False) -> Iterator[LexborNode]:
376
+ """Iterate over nodes on the current level.
377
+
378
+ Parameters
379
+ ----------
380
+ include_text : bool
381
+ If True, includes text nodes as well.
382
+
383
+ Yields
384
+ -------
385
+ node
386
+ """
387
+ ...
388
+ def unwrap(self, delete_empty: bool = False) -> None:
389
+ """Replace node with whatever is inside this node.
390
+
391
+ Does nothing if you perform unwrapping second time on the same node.
392
+
393
+ Parameters
394
+ ----------
395
+ delete_empty : bool, default False
396
+ If True, removes empty tags.
397
+
398
+ Examples
399
+ --------
400
+
401
+ >>> tree = LexborHTMLParser("<div>Hello <i>world</i>!</div>")
402
+ >>> tree.css_first('i').unwrap()
403
+ >>> tree.html
404
+ '<html><head></head><body><div>Hello world!</div></body></html>'
405
+
406
+ Note: by default, empty tags are ignored, use "delete_empty" to change this.
407
+ """
408
+ ...
409
+ def unwrap_tags(self, tags: list[str], delete_empty: bool = False) -> None:
410
+ """Unwraps specified tags from the HTML tree.
411
+
412
+ Works the same as the ``unwrap`` method, but applied to a list of tags.
413
+
414
+ Parameters
415
+ ----------
416
+ tags : list
417
+ List of tags to remove.
418
+ delete_empty : bool, default False
419
+ If True, removes empty tags.
420
+
421
+ Examples
422
+ --------
423
+
424
+ >>> tree = LexborHTMLParser("<div><a href="">Hello</a> <i>world</i>!</div>")
425
+ >>> tree.body.unwrap_tags(['i','a'])
426
+ >>> tree.body.html
427
+ '<body><div>Hello world!</div></body>'
428
+
429
+ Note: by default, empty tags are ignored, use "delete_empty" to change this.
430
+ """
431
+ ...
432
+ def merge_text_nodes(self) -> None:
433
+ """Iterates over all text nodes and merges all text nodes that are close to each other.
434
+
435
+ This is useful for text extraction.
436
+ Use it when you need to strip HTML tags and merge "dangling" text.
437
+
438
+ Examples
439
+ --------
440
+
441
+ >>> tree = LexborHTMLParser("<div><p><strong>J</strong>ohn</p><p>Doe</p></div>")
442
+ >>> node = tree.css_first('div')
443
+ >>> tree.unwrap_tags(["strong"])
444
+ >>> tree.text(deep=True, separator=" ", strip=True)
445
+ "J ohn Doe" # Text extraction produces an extra space because the strong tag was removed.
446
+ >>> node.merge_text_nodes()
447
+ >>> tree.text(deep=True, separator=" ", strip=True)
448
+ "John Doe"
449
+ """
450
+ ...
451
+ def traverse(self, include_text: bool = False) -> Iterator[LexborNode]:
452
+ """Iterate over all child and next nodes starting from the current level.
453
+
454
+ Parameters
455
+ ----------
456
+ include_text : bool
457
+ If True, includes text nodes as well.
458
+
459
+ Yields
460
+ -------
461
+ node
462
+ """
463
+ ...
464
+ def replace_with(self, value: bytes | str | LexborNode) -> None:
465
+ """Replace current Node with specified value.
466
+
467
+ Parameters
468
+ ----------
469
+ value : str, bytes or Node
470
+ The text or Node instance to replace the Node with.
471
+ When a text string is passed, it's treated as text. All HTML tags will be escaped.
472
+ Convert and pass the ``Node`` object when you want to work with HTML.
473
+ Does not clone the ``Node`` object.
474
+ All future changes to the passed ``Node`` object will also be taken into account.
475
+
476
+ Examples
477
+ --------
478
+
479
+ >>> tree = LexborHTMLParser('<div>Get <img src="" alt="Laptop"></div>')
480
+ >>> img = tree.css_first('img')
481
+ >>> img.replace_with(img.attributes.get('alt', ''))
482
+ >>> tree.body.child.html
483
+ '<div>Get Laptop</div>'
484
+
485
+ >>> html_parser = LexborHTMLParser('<div>Get <span alt="Laptop"><img src="/jpg"> <div></div></span></div>')
486
+ >>> html_parser2 = LexborHTMLParser('<div>Test</div>')
487
+ >>> img_node = html_parser.css_first('img')
488
+ >>> img_node.replace_with(html_parser2.body.child)
489
+ '<div>Get <span alt="Laptop"><div>Test</div> <div></div></span></div>'
490
+ """
491
+ ...
492
+ def insert_before(self, value: bytes | str | LexborNode) -> None:
493
+ """Insert a node before the current Node.
494
+
495
+ Parameters
496
+ ----------
497
+ value : str, bytes or Node
498
+ The text or Node instance to insert before the Node.
499
+ When a text string is passed, it's treated as text. All HTML tags will be escaped.
500
+ Convert and pass the ``Node`` object when you want to work with HTML.
501
+ Does not clone the ``Node`` object.
502
+ All future changes to the passed ``Node`` object will also be taken into account.
503
+
504
+ Examples
505
+ --------
506
+
507
+ >>> tree = LexborHTMLParser('<div>Get <img src="" alt="Laptop"></div>')
508
+ >>> img = tree.css_first('img')
509
+ >>> img.insert_before(img.attributes.get('alt', ''))
510
+ >>> tree.body.child.html
511
+ '<div>Get Laptop<img src="" alt="Laptop"></div>'
512
+
513
+ >>> html_parser = LexborHTMLParser('<div>Get <span alt="Laptop"><img src="/jpg"> <div></div></span></div>')
514
+ >>> html_parser2 = LexborHTMLParser('<div>Test</div>')
515
+ >>> img_node = html_parser.css_first('img')
516
+ >>> img_node.insert_before(html_parser2.body.child)
517
+ <div>Get <span alt="Laptop"><div>Test</div><img src="/jpg"> <div></div></span></div>'
518
+ """
519
+ ...
520
+ def insert_after(self, value: bytes | str | LexborNode) -> None:
521
+ """Insert a node after the current Node.
522
+
523
+ Parameters
524
+ ----------
525
+ value : str, bytes or Node
526
+ The text or Node instance to insert after the Node.
527
+ When a text string is passed, it's treated as text. All HTML tags will be escaped.
528
+ Convert and pass the ``Node`` object when you want to work with HTML.
529
+ Does not clone the ``Node`` object.
530
+ All future changes to the passed ``Node`` object will also be taken into account.
531
+
532
+ Examples
533
+ --------
534
+
535
+ >>> tree = LexborHTMLParser('<div>Get <img src="" alt="Laptop"></div>')
536
+ >>> img = tree.css_first('img')
537
+ >>> img.insert_after(img.attributes.get('alt', ''))
538
+ >>> tree.body.child.html
539
+ '<div>Get <img src="" alt="Laptop">Laptop</div>'
540
+
541
+ >>> html_parser = LexborHTMLParser('<div>Get <span alt="Laptop"><img src="/jpg"> <div></div></span></div>')
542
+ >>> html_parser2 = LexborHTMLParser('<div>Test</div>')
543
+ >>> img_node = html_parser.css_first('img')
544
+ >>> img_node.insert_after(html_parser2.body.child)
545
+ <div>Get <span alt="Laptop"><img src="/jpg"><div>Test</div> <div></div></span></div>'
546
+ """
547
+ ...
548
+ def insert_child(self, value: bytes | str | LexborNode) -> None:
549
+ """Insert a node inside (at the end of) the current Node.
550
+
551
+ Parameters
552
+ ----------
553
+ value : str, bytes or Node
554
+ The text or Node instance to insert inside the Node.
555
+ When a text string is passed, it's treated as text. All HTML tags will be escaped.
556
+ Convert and pass the ``Node`` object when you want to work with HTML.
557
+ Does not clone the ``Node`` object.
558
+ All future changes to the passed ``Node`` object will also be taken into account.
559
+
560
+ Examples
561
+ --------
562
+
563
+ >>> tree = LexborHTMLParser('<div>Get <img src=""></div>')
564
+ >>> div = tree.css_first('div')
565
+ >>> div.insert_child('Laptop')
566
+ >>> tree.body.child.html
567
+ '<div>Get <img src="">Laptop</div>'
568
+
569
+ >>> html_parser = LexborHTMLParser('<div>Get <span alt="Laptop"> <div>Laptop</div> </span></div>')
570
+ >>> html_parser2 = LexborHTMLParser('<div>Test</div>')
571
+ >>> span_node = html_parser.css_first('span')
572
+ >>> span_node.insert_child(html_parser2.body.child)
573
+ <div>Get <span alt="Laptop"> <div>Laptop</div> <div>Test</div> </span></div>'
574
+ """
575
+ ...
110
576
  @property
111
- def raw_value(self) -> NoReturn: ...
112
- def scripts_contain(self, query: str) -> bool: ...
113
- def scripts_srcs_contain(self, queries: tuple[str]) -> bool: ...
114
- def remove(self, recursive: bool = True) -> None: ...
115
- def select(self, query: str | None = None) -> LexborSelector: ...
577
+ def raw_value(self) -> NoReturn:
578
+ """Return the raw (unparsed, original) value of a node.
579
+
580
+ Currently, works on text nodes only.
581
+
582
+ Returns
583
+ -------
584
+
585
+ raw_value : bytes
586
+
587
+ Examples
588
+ --------
589
+
590
+ >>> html_parser = LexborHTMLParser('<div>&#x3C;test&#x3E;</div>')
591
+ >>> selector = html_parser.css_first('div')
592
+ >>> selector.child.html
593
+ '&lt;test&gt;'
594
+ >>> selector.child.raw_value
595
+ b'&#x3C;test&#x3E;'
596
+ """
597
+ ...
598
+ def scripts_contain(self, query: str) -> bool:
599
+ """Returns True if any of the script tags contain specified text.
600
+
601
+ Caches script tags on the first call to improve performance.
602
+
603
+ Parameters
604
+ ----------
605
+ query : str
606
+ The query to check.
607
+ """
608
+ ...
609
+ def script_srcs_contain(self, queries: tuple[str]) -> bool:
610
+ """Returns True if any of the script SRCs attributes contain on of the specified text.
611
+
612
+ Caches values on the first call to improve performance.
613
+
614
+ Parameters
615
+ ----------
616
+ queries : tuple of str
617
+ """
618
+ ...
619
+ def remove(self, recursive: bool = True) -> None:
620
+ """An alias for the decompose method."""
621
+ ...
622
+ def select(self, query: str | None = None) -> LexborSelector:
623
+ """Select nodes given a CSS selector.
624
+
625
+ Works similarly to the the ``css`` method, but supports chained filtering and extra features.
626
+
627
+ Parameters
628
+ ----------
629
+ query : str or None
630
+ The CSS selector to use when searching for nodes.
631
+
632
+ Returns
633
+ -------
634
+ selector : The `Selector` class.
635
+ """
636
+ ...
637
+ @property
638
+ def text_content(self) -> str | None:
639
+ """Returns the text of the node if it is a text node.
640
+
641
+ Returns None for other nodes.
642
+ Unlike the ``text`` method, does not include child nodes.
643
+
644
+ Returns
645
+ -------
646
+ text : str or None.
647
+ """
648
+ ...
649
+
116
650
  @property
117
- def text_content(self) -> str | None: ...
651
+ def inner_html(self) -> str | None:
652
+ """Return HTML representation of the child nodes.
653
+
654
+ Works similar to innerHTML in JavaScript.
655
+ Unlike the `.html` property, does not include the current node.
656
+ Can be used to set HTML as well. See the setter docstring.
657
+
658
+ Returns
659
+ -------
660
+ text : str or None
661
+ """
662
+ ...
663
+
664
+ @inner_html.setter
665
+ def inner_html(self, html: str):
666
+ """Set inner HTML to the specified HTML.
667
+
668
+ Replaces existing data inside the node.
669
+ Works similar to innerHTML in JavaScript.
670
+
671
+ Parameters
672
+ ----------
673
+ html : str
674
+
675
+ """
676
+ ...
677
+
678
+ def clone(self) -> LexborNode:
679
+ """Clone the current node.
680
+
681
+ You can use to do temporary modifications without affecting the original HTML tree.
682
+
683
+ It is tied to the current parser instance.
684
+ Gets destroyed when parser instance is destroyed.
685
+ """
686
+ ...
118
687
 
119
688
  class LexborHTMLParser:
120
- def __init__(self, html: str| bytes ): ...
689
+ """The lexbor HTML parser.
690
+
691
+ Use this class to parse raw HTML.
692
+
693
+ This parser mimics most of the stuff from ``HTMLParser`` but not inherits it directly.
694
+
695
+ Parameters
696
+ ----------
697
+
698
+ html : str (unicode) or bytes
699
+ """
700
+
701
+ def __init__(self, html: str | bytes): ...
121
702
  @property
122
703
  def selector(self) -> "LexborCSSSelector": ...
123
704
  @property
124
- def root(self) -> LexborNode | None: ...
705
+ def root(self) -> LexborNode | None:
706
+ """Returns root node."""
707
+ ...
125
708
  @property
126
- def body(self) -> LexborNode | None: ...
709
+ def body(self) -> LexborNode | None:
710
+ """Returns document body."""
711
+ ...
127
712
  @property
128
- def head(self) -> LexborNode | None: ...
129
- def tags(self, name: str) -> list[LexborNode]: ...
130
- def text(
131
- self, deep: bool = True, separator: str = "", strip: bool = False
132
- ) -> str: ...
713
+ def head(self) -> LexborNode | None:
714
+ """Returns document head."""
715
+ ...
716
+ def tags(self, name: str) -> list[LexborNode]:
717
+ """Returns a list of tags that match specified name.
718
+
719
+ Parameters
720
+ ----------
721
+ name : str (e.g. div)
722
+ """
723
+ ...
724
+ def text(self, deep: bool = True, separator: str = "", strip: bool = False) -> str:
725
+ """Returns the text of the node including text of all its child nodes.
726
+
727
+ Parameters
728
+ ----------
729
+ strip : bool, default False
730
+ If true, calls ``str.strip()`` on each text part to remove extra white spaces.
731
+ separator : str, default ''
732
+ The separator to use when joining text from different nodes.
733
+ deep : bool, default True
734
+ If True, includes text from all child nodes.
735
+
736
+ Returns
737
+ -------
738
+ text : str
739
+ """
740
+ ...
133
741
  @property
134
- def html(self) -> str | None: ...
135
- def css(self, query: str) -> list[LexborNode]: ...
742
+ def html(self) -> str | None:
743
+ """Return HTML representation of the page."""
744
+ ...
745
+ def css(self, query: str) -> list[LexborNode]:
746
+ """A CSS selector.
747
+
748
+ Matches pattern `query` against HTML tree.
749
+ `CSS selectors reference <https://www.w3schools.com/cssref/css_selectors.asp>`_.
750
+
751
+ Special selectors:
752
+
753
+ - parser.css('p:lexbor-contains("awesome" i)') -- case-insensitive contains
754
+ - parser.css('p:lexbor-contains("awesome")') -- case-sensitive contains
755
+
756
+
757
+ Parameters
758
+ ----------
759
+ query : str
760
+ CSS selector (e.g. "div > :nth-child(2n+1):not(:has(a))").
761
+
762
+ Returns
763
+ -------
764
+ selector : list of `Node` objects
765
+ """
766
+ ...
136
767
  @overload
137
768
  def css_first(
138
769
  self, query: str, default: Any = ..., strict: Literal[True] = ...
139
- ) -> LexborNode: ...
770
+ ) -> LexborNode:
771
+ """Same as `css` but returns only the first match.
772
+
773
+ Parameters
774
+ ----------
775
+
776
+ query : str
777
+ default : bool, default None
778
+ Default value to return if there is no match.
779
+ strict: bool, default False
780
+ Set to True if you want to check if there is strictly only one match in the document.
781
+
782
+
783
+ Returns
784
+ -------
785
+ selector : `LexborNode` object
786
+ """
787
+ ...
140
788
  @overload
141
789
  def css_first(
142
790
  self, query: str, default: DefaultT, strict: bool = False
143
- ) -> LexborNode | DefaultT: ...
791
+ ) -> LexborNode | DefaultT:
792
+ """Same as `css` but returns only the first match.
793
+
794
+ Parameters
795
+ ----------
796
+
797
+ query : str
798
+ default : bool, default None
799
+ Default value to return if there is no match.
800
+ strict: bool, default False
801
+ Set to True if you want to check if there is strictly only one match in the document.
802
+
803
+
804
+ Returns
805
+ -------
806
+ selector : `LexborNode` object
807
+ """
808
+ ...
144
809
  @overload
145
810
  def css_first(
146
811
  self, query: str, default: None = ..., strict: bool = False
147
- ) -> LexborNode | None: ...
148
- def strip_tags(self, tags: list[str], recursive: bool = False) -> None: ...
149
- def select(self, query: str | None = None) -> LexborSelector | None: ...
150
- def any_css_matches(self, selectors: tuple[str]) -> bool: ...
151
- def scripts_contain(self, query: str) -> bool: ...
152
- def scripts_srcs_contain(self, queries: tuple[str]) -> bool: ...
812
+ ) -> LexborNode | None:
813
+ """Same as `css` but returns only the first match.
814
+
815
+ Parameters
816
+ ----------
817
+
818
+ query : str
819
+ default : bool, default None
820
+ Default value to return if there is no match.
821
+ strict: bool, default False
822
+ Set to True if you want to check if there is strictly only one match in the document.
823
+
824
+
825
+ Returns
826
+ -------
827
+ selector : `LexborNode` object
828
+ """
829
+ ...
830
+ def strip_tags(self, tags: list[str], recursive: bool = False) -> None:
831
+ """Remove specified tags from the node.
832
+
833
+ Parameters
834
+ ----------
835
+ tags : list of str
836
+ List of tags to remove.
837
+ recursive : bool, default True
838
+ Whenever to delete all its child nodes
839
+
840
+ Examples
841
+ --------
842
+
843
+ >>> tree = LexborHTMLParser('<html><head></head><body><script></script><div>Hello world!</div></body></html>')
844
+ >>> tags = ['head', 'style', 'script', 'xmp', 'iframe', 'noembed', 'noframes']
845
+ >>> tree.strip_tags(tags)
846
+ >>> tree.html
847
+ '<html><body><div>Hello world!</div></body></html>'
848
+ """
849
+ ...
850
+ def select(self, query: str | None = None) -> LexborSelector | None:
851
+ """Select nodes give a CSS selector.
852
+
853
+ Works similarly to the ``css`` method, but supports chained filtering and extra features.
854
+
855
+ Parameters
856
+ ----------
857
+ query : str or None
858
+ The CSS selector to use when searching for nodes.
859
+
860
+ Returns
861
+ -------
862
+ selector : The `Selector` class.
863
+ """
864
+ ...
865
+ def any_css_matches(self, selectors: tuple[str]) -> bool:
866
+ """Returns True if any of the specified CSS selectors matches a node."""
867
+ ...
868
+ def scripts_contain(self, query: str) -> bool:
869
+ """Returns True if any of the script tags contain specified text.
870
+
871
+ Caches script tags on the first call to improve performance.
872
+
873
+ Parameters
874
+ ----------
875
+ query : str
876
+ The query to check.
877
+ """
878
+ ...
879
+ def script_srcs_contain(self, queries: tuple[str]) -> bool:
880
+ """Returns True if any of the script SRCs attributes contain on of the specified text.
881
+
882
+ Caches values on the first call to improve performance.
883
+
884
+ Parameters
885
+ ----------
886
+ queries : tuple of str
887
+ """
888
+ ...
153
889
  def css_matches(self, selector: str) -> bool: ...
154
- def clone(self) -> LexborHTMLParser: ...
155
- def unwrap_tags(self, tags: list[str], delete_empty : bool = False) -> None: ...
890
+ def merge_text_nodes(self) -> None:
891
+ """Iterates over all text nodes and merges all text nodes that are close to each other.
892
+
893
+ This is useful for text extraction.
894
+ Use it when you need to strip HTML tags and merge "dangling" text.
895
+
896
+ Examples
897
+ --------
898
+
899
+ >>> tree = LexborHTMLParser("<div><p><strong>J</strong>ohn</p><p>Doe</p></div>")
900
+ >>> node = tree.css_first('div')
901
+ >>> tree.unwrap_tags(["strong"])
902
+ >>> tree.text(deep=True, separator=" ", strip=True)
903
+ "J ohn Doe" # Text extraction produces an extra space because the strong tag was removed.
904
+ >>> node.merge_text_nodes()
905
+ >>> tree.text(deep=True, separator=" ", strip=True)
906
+ "John Doe"
907
+ """
908
+ ...
909
+ def clone(self) -> LexborHTMLParser:
910
+ """Clone the current tree."""
911
+ ...
912
+ def unwrap_tags(self, tags: list[str], delete_empty: bool = False) -> None:
913
+ """Unwraps specified tags from the HTML tree.
914
+
915
+ Works the same as the ``unwrap`` method, but applied to a list of tags.
916
+
917
+ Parameters
918
+ ----------
919
+ tags : list
920
+ List of tags to remove.
921
+ delete_empty : bool
922
+ Whenever to delete empty tags.
923
+
924
+ Examples
925
+ --------
926
+
927
+ >>> tree = LexborHTMLParser("<div><a href="">Hello</a> <i>world</i>!</div>")
928
+ >>> tree.body.unwrap_tags(['i','a'])
929
+ >>> tree.body.html
930
+ '<body><div>Hello world!</div></body>'
931
+ """
932
+ ...
156
933
 
157
934
  def create_tag(tag: str) -> LexborNode:
158
935
  """
@@ -170,3 +947,8 @@ def parse_fragment(html: str) -> list[LexborNode]:
170
947
  if they are missing. This function does not add these tags.
171
948
  """
172
949
  ...
950
+
951
+ class SelectolaxError(Exception):
952
+ """An exception that indicates error."""
953
+
954
+ pass