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