selectolax 0.4.0__cp314-cp314t-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/__init__.py +8 -0
- selectolax/base.pxi +4 -0
- selectolax/lexbor/attrs.pxi +120 -0
- selectolax/lexbor/node.pxi +1034 -0
- selectolax/lexbor/node_remove.pxi +29 -0
- selectolax/lexbor/selection.pxi +215 -0
- selectolax/lexbor/util.pxi +20 -0
- selectolax/lexbor.c +54897 -0
- selectolax/lexbor.cpython-314t-aarch64-linux-musl.so +0 -0
- selectolax/lexbor.pxd +584 -0
- selectolax/lexbor.pyi +954 -0
- selectolax/lexbor.pyx +424 -0
- selectolax/modest/node.pxi +991 -0
- selectolax/modest/selection.pxi +195 -0
- selectolax/modest/util.pxi +20 -0
- selectolax/parser.c +50279 -0
- selectolax/parser.cpython-314t-aarch64-linux-musl.so +0 -0
- selectolax/parser.pxd +578 -0
- selectolax/parser.pyi +770 -0
- selectolax/parser.pyx +443 -0
- selectolax/py.typed +0 -0
- selectolax/utils.pxi +117 -0
- selectolax-0.4.0.dist-info/METADATA +32 -0
- selectolax-0.4.0.dist-info/RECORD +27 -0
- selectolax-0.4.0.dist-info/WHEEL +5 -0
- selectolax-0.4.0.dist-info/licenses/LICENSE +10 -0
- selectolax-0.4.0.dist-info/top_level.txt +1 -0
selectolax/lexbor.pyx
ADDED
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
from cpython.bool cimport bool
|
|
2
|
+
from cpython.exc cimport PyErr_SetObject
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
_ENCODING = 'UTF-8'
|
|
6
|
+
|
|
7
|
+
include "base.pxi"
|
|
8
|
+
include "utils.pxi"
|
|
9
|
+
include "lexbor/attrs.pxi"
|
|
10
|
+
include "lexbor/node.pxi"
|
|
11
|
+
include "lexbor/selection.pxi"
|
|
12
|
+
include "lexbor/util.pxi"
|
|
13
|
+
include "lexbor/node_remove.pxi"
|
|
14
|
+
|
|
15
|
+
# We don't inherit from HTMLParser here, because it also includes all the C code from Modest.
|
|
16
|
+
|
|
17
|
+
cdef class LexborHTMLParser:
|
|
18
|
+
"""The lexbor HTML parser.
|
|
19
|
+
|
|
20
|
+
Use this class to parse raw HTML.
|
|
21
|
+
|
|
22
|
+
This parser mimics most of the stuff from ``HTMLParser`` but not inherits it directly.
|
|
23
|
+
|
|
24
|
+
Parameters
|
|
25
|
+
----------
|
|
26
|
+
|
|
27
|
+
html : str (unicode) or bytes
|
|
28
|
+
"""
|
|
29
|
+
def __init__(self, html):
|
|
30
|
+
cdef size_t html_len
|
|
31
|
+
cdef object bytes_html
|
|
32
|
+
bytes_html, html_len = preprocess_input(html)
|
|
33
|
+
self._parse_html(bytes_html, html_len)
|
|
34
|
+
self.raw_html = bytes_html
|
|
35
|
+
self._selector = None
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def selector(self):
|
|
39
|
+
if self._selector is None:
|
|
40
|
+
self._selector = LexborCSSSelector()
|
|
41
|
+
return self._selector
|
|
42
|
+
|
|
43
|
+
cdef int _parse_html(self, char *html, size_t html_len) except -1:
|
|
44
|
+
cdef lxb_status_t status
|
|
45
|
+
|
|
46
|
+
with nogil:
|
|
47
|
+
self.document = lxb_html_document_create()
|
|
48
|
+
|
|
49
|
+
if self.document == NULL:
|
|
50
|
+
PyErr_SetObject(SelectolaxError, "Failed to initialize object for HTML Document.")
|
|
51
|
+
return -1
|
|
52
|
+
|
|
53
|
+
with nogil:
|
|
54
|
+
status = lxb_html_document_parse(self.document, <lxb_char_t *> html, html_len)
|
|
55
|
+
|
|
56
|
+
if status != 0x0000:
|
|
57
|
+
PyErr_SetObject(SelectolaxError, "Can't parse HTML.")
|
|
58
|
+
return -1
|
|
59
|
+
|
|
60
|
+
if self.document == NULL:
|
|
61
|
+
PyErr_SetObject(RuntimeError, "document is NULL even after html was parsed correctly")
|
|
62
|
+
return -1
|
|
63
|
+
return 0
|
|
64
|
+
|
|
65
|
+
def __dealloc__(self):
|
|
66
|
+
if self.document != NULL:
|
|
67
|
+
lxb_html_document_destroy(self.document)
|
|
68
|
+
|
|
69
|
+
def __repr__(self):
|
|
70
|
+
return '<LexborHTMLParser chars=%s>' % len(self.root.html)
|
|
71
|
+
|
|
72
|
+
@property
|
|
73
|
+
def root(self):
|
|
74
|
+
"""Returns root node."""
|
|
75
|
+
if self.document == NULL:
|
|
76
|
+
return None
|
|
77
|
+
return LexborNode.new(<lxb_dom_node_t *> lxb_dom_document_root(&self.document.dom_document), self)
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def body(self):
|
|
81
|
+
"""Returns document body."""
|
|
82
|
+
cdef lxb_html_body_element_t* body
|
|
83
|
+
body = lxb_html_document_body_element_noi(self.document)
|
|
84
|
+
if body == NULL:
|
|
85
|
+
return None
|
|
86
|
+
return LexborNode.new(<lxb_dom_node_t *> body, self)
|
|
87
|
+
|
|
88
|
+
@property
|
|
89
|
+
def head(self):
|
|
90
|
+
"""Returns document head."""
|
|
91
|
+
cdef lxb_html_head_element_t* head
|
|
92
|
+
head = lxb_html_document_head_element_noi(self.document)
|
|
93
|
+
if head == NULL:
|
|
94
|
+
return None
|
|
95
|
+
return LexborNode.new(<lxb_dom_node_t *> head, self)
|
|
96
|
+
|
|
97
|
+
def tags(self, str name):
|
|
98
|
+
"""Returns a list of tags that match specified name.
|
|
99
|
+
|
|
100
|
+
Parameters
|
|
101
|
+
----------
|
|
102
|
+
name : str (e.g. div)
|
|
103
|
+
|
|
104
|
+
"""
|
|
105
|
+
|
|
106
|
+
if not name:
|
|
107
|
+
raise ValueError("Tag name cannot be empty")
|
|
108
|
+
if len(name) > 100:
|
|
109
|
+
raise ValueError("Tag name is too long")
|
|
110
|
+
|
|
111
|
+
cdef lxb_dom_collection_t* collection = NULL
|
|
112
|
+
cdef lxb_status_t status
|
|
113
|
+
pybyte_name = name.encode('UTF-8')
|
|
114
|
+
|
|
115
|
+
result = list()
|
|
116
|
+
collection = lxb_dom_collection_make(&self.document.dom_document, 128)
|
|
117
|
+
|
|
118
|
+
if collection == NULL:
|
|
119
|
+
return result
|
|
120
|
+
status = lxb_dom_elements_by_tag_name(
|
|
121
|
+
<lxb_dom_element_t *> self.document,
|
|
122
|
+
collection,
|
|
123
|
+
<lxb_char_t *> pybyte_name,
|
|
124
|
+
len(pybyte_name)
|
|
125
|
+
)
|
|
126
|
+
if status != 0x0000:
|
|
127
|
+
lxb_dom_collection_destroy(collection, <bint> True)
|
|
128
|
+
raise SelectolaxError("Can't locate elements.")
|
|
129
|
+
|
|
130
|
+
for i in range(lxb_dom_collection_length_noi(collection)):
|
|
131
|
+
node = LexborNode.new(
|
|
132
|
+
<lxb_dom_node_t*> lxb_dom_collection_element_noi(collection, i),
|
|
133
|
+
self
|
|
134
|
+
)
|
|
135
|
+
result.append(node)
|
|
136
|
+
lxb_dom_collection_destroy(collection, <bint> True)
|
|
137
|
+
return result
|
|
138
|
+
|
|
139
|
+
def text(self, bool deep=True, str separator='', bool strip=False):
|
|
140
|
+
"""Returns the text of the node including text of all its child nodes.
|
|
141
|
+
|
|
142
|
+
Parameters
|
|
143
|
+
----------
|
|
144
|
+
strip : bool, default False
|
|
145
|
+
If true, calls ``str.strip()`` on each text part to remove extra white spaces.
|
|
146
|
+
separator : str, default ''
|
|
147
|
+
The separator to use when joining text from different nodes.
|
|
148
|
+
deep : bool, default True
|
|
149
|
+
If True, includes text from all child nodes.
|
|
150
|
+
|
|
151
|
+
Returns
|
|
152
|
+
-------
|
|
153
|
+
text : str
|
|
154
|
+
|
|
155
|
+
"""
|
|
156
|
+
if self.body is None:
|
|
157
|
+
return ""
|
|
158
|
+
return self.body.text(deep=deep, separator=separator, strip=strip)
|
|
159
|
+
|
|
160
|
+
@property
|
|
161
|
+
def html(self):
|
|
162
|
+
"""Return HTML representation of the page."""
|
|
163
|
+
if self.document == NULL:
|
|
164
|
+
return None
|
|
165
|
+
node = LexborNode.new(<lxb_dom_node_t *> &self.document.dom_document, self)
|
|
166
|
+
return node.html
|
|
167
|
+
|
|
168
|
+
def css(self, str query):
|
|
169
|
+
"""A CSS selector.
|
|
170
|
+
|
|
171
|
+
Matches pattern `query` against HTML tree.
|
|
172
|
+
`CSS selectors reference <https://www.w3schools.com/cssref/css_selectors.asp>`_.
|
|
173
|
+
|
|
174
|
+
Special selectors:
|
|
175
|
+
|
|
176
|
+
- parser.css('p:lexbor-contains("awesome" i)') -- case-insensitive contains
|
|
177
|
+
- parser.css('p:lexbor-contains("awesome")') -- case-sensitive contains
|
|
178
|
+
|
|
179
|
+
Parameters
|
|
180
|
+
----------
|
|
181
|
+
query : str
|
|
182
|
+
CSS selector (e.g. "div > :nth-child(2n+1):not(:has(a))").
|
|
183
|
+
|
|
184
|
+
Returns
|
|
185
|
+
-------
|
|
186
|
+
selector : list of `Node` objects
|
|
187
|
+
"""
|
|
188
|
+
return self.root.css(query)
|
|
189
|
+
|
|
190
|
+
def css_first(self, str query, default=None, strict=False):
|
|
191
|
+
"""Same as `css` but returns only the first match.
|
|
192
|
+
|
|
193
|
+
Parameters
|
|
194
|
+
----------
|
|
195
|
+
|
|
196
|
+
query : str
|
|
197
|
+
default : Any, default None
|
|
198
|
+
Default value to return if there is no match.
|
|
199
|
+
strict: bool, default False
|
|
200
|
+
Set to True if you want to check if there is strictly only one match in the document.
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
Returns
|
|
204
|
+
-------
|
|
205
|
+
selector : `LexborNode` object
|
|
206
|
+
"""
|
|
207
|
+
return self.root.css_first(query, default, strict)
|
|
208
|
+
|
|
209
|
+
def strip_tags(self, list tags, bool recursive = False):
|
|
210
|
+
"""Remove specified tags from the node.
|
|
211
|
+
|
|
212
|
+
Parameters
|
|
213
|
+
----------
|
|
214
|
+
tags : list of str
|
|
215
|
+
List of tags to remove.
|
|
216
|
+
recursive : bool, default False
|
|
217
|
+
Whenever to delete all its child nodes
|
|
218
|
+
|
|
219
|
+
Examples
|
|
220
|
+
--------
|
|
221
|
+
|
|
222
|
+
>>> tree = LexborHTMLParser('<html><head></head><body><script></script><div>Hello world!</div></body></html>')
|
|
223
|
+
>>> tags = ['head', 'style', 'script', 'xmp', 'iframe', 'noembed', 'noframes']
|
|
224
|
+
>>> tree.strip_tags(tags)
|
|
225
|
+
>>> tree.html
|
|
226
|
+
'<html><body><div>Hello world!</div></body></html>'
|
|
227
|
+
|
|
228
|
+
"""
|
|
229
|
+
cdef lxb_dom_collection_t* collection = NULL
|
|
230
|
+
cdef lxb_status_t status
|
|
231
|
+
|
|
232
|
+
for tag in tags:
|
|
233
|
+
pybyte_name = tag.encode('UTF-8')
|
|
234
|
+
|
|
235
|
+
collection = lxb_dom_collection_make(&self.document.dom_document, 128)
|
|
236
|
+
|
|
237
|
+
if collection == NULL:
|
|
238
|
+
raise SelectolaxError("Can't initialize DOM collection.")
|
|
239
|
+
|
|
240
|
+
status = lxb_dom_elements_by_tag_name(
|
|
241
|
+
<lxb_dom_element_t *> self.document,
|
|
242
|
+
collection,
|
|
243
|
+
<lxb_char_t *> pybyte_name,
|
|
244
|
+
len(pybyte_name)
|
|
245
|
+
)
|
|
246
|
+
if status != 0x0000:
|
|
247
|
+
lxb_dom_collection_destroy(collection, <bint> True)
|
|
248
|
+
raise SelectolaxError("Can't locate elements.")
|
|
249
|
+
|
|
250
|
+
for i in range(lxb_dom_collection_length_noi(collection)):
|
|
251
|
+
if recursive:
|
|
252
|
+
lxb_dom_node_destroy_deep(<lxb_dom_node_t*> lxb_dom_collection_element_noi(collection, i))
|
|
253
|
+
else:
|
|
254
|
+
lxb_dom_node_destroy(<lxb_dom_node_t *> lxb_dom_collection_element_noi(collection, i))
|
|
255
|
+
lxb_dom_collection_destroy(collection, <bint> True)
|
|
256
|
+
|
|
257
|
+
def select(self, query=None):
|
|
258
|
+
"""Select nodes give a CSS selector.
|
|
259
|
+
|
|
260
|
+
Works similarly to the ``css`` method, but supports chained filtering and extra features.
|
|
261
|
+
|
|
262
|
+
Parameters
|
|
263
|
+
----------
|
|
264
|
+
query : str or None
|
|
265
|
+
The CSS selector to use when searching for nodes.
|
|
266
|
+
|
|
267
|
+
Returns
|
|
268
|
+
-------
|
|
269
|
+
selector : The `Selector` class.
|
|
270
|
+
"""
|
|
271
|
+
cdef LexborNode node
|
|
272
|
+
node = self.root
|
|
273
|
+
if node:
|
|
274
|
+
return LexborSelector(node, query)
|
|
275
|
+
|
|
276
|
+
def any_css_matches(self, tuple selectors):
|
|
277
|
+
"""Returns True if any of the specified CSS selectors matches a node."""
|
|
278
|
+
return self.root.any_css_matches(selectors)
|
|
279
|
+
|
|
280
|
+
def scripts_contain(self, str query):
|
|
281
|
+
"""Returns True if any of the script tags contain specified text.
|
|
282
|
+
|
|
283
|
+
Caches script tags on the first call to improve performance.
|
|
284
|
+
|
|
285
|
+
Parameters
|
|
286
|
+
----------
|
|
287
|
+
query : str
|
|
288
|
+
The query to check.
|
|
289
|
+
|
|
290
|
+
"""
|
|
291
|
+
return self.root.scripts_contain(query)
|
|
292
|
+
|
|
293
|
+
def script_srcs_contain(self, tuple queries):
|
|
294
|
+
"""Returns True if any of the script SRCs attributes contain on of the specified text.
|
|
295
|
+
|
|
296
|
+
Caches values on the first call to improve performance.
|
|
297
|
+
|
|
298
|
+
Parameters
|
|
299
|
+
----------
|
|
300
|
+
queries : tuple of str
|
|
301
|
+
|
|
302
|
+
"""
|
|
303
|
+
return self.root.script_srcs_contain(queries)
|
|
304
|
+
|
|
305
|
+
def css_matches(self, str selector):
|
|
306
|
+
return self.root.css_matches(selector)
|
|
307
|
+
|
|
308
|
+
def merge_text_nodes(self):
|
|
309
|
+
"""Iterates over all text nodes and merges all text nodes that are close to each other.
|
|
310
|
+
|
|
311
|
+
This is useful for text extraction.
|
|
312
|
+
Use it when you need to strip HTML tags and merge "dangling" text.
|
|
313
|
+
|
|
314
|
+
Examples
|
|
315
|
+
--------
|
|
316
|
+
|
|
317
|
+
>>> tree = LexborHTMLParser("<div><p><strong>J</strong>ohn</p><p>Doe</p></div>")
|
|
318
|
+
>>> node = tree.css_first('div')
|
|
319
|
+
>>> tree.unwrap_tags(["strong"])
|
|
320
|
+
>>> tree.text(deep=True, separator=" ", strip=True)
|
|
321
|
+
"J ohn Doe" # Text extraction produces an extra space because the strong tag was removed.
|
|
322
|
+
>>> node.merge_text_nodes()
|
|
323
|
+
>>> tree.text(deep=True, separator=" ", strip=True)
|
|
324
|
+
"John Doe"
|
|
325
|
+
"""
|
|
326
|
+
return self.root.merge_text_nodes()
|
|
327
|
+
|
|
328
|
+
@staticmethod
|
|
329
|
+
cdef LexborHTMLParser from_document(lxb_html_document_t *document, bytes raw_html):
|
|
330
|
+
obj = <LexborHTMLParser> LexborHTMLParser.__new__(LexborHTMLParser)
|
|
331
|
+
obj.document = document
|
|
332
|
+
obj.raw_html = raw_html
|
|
333
|
+
obj.cached_script_texts = None
|
|
334
|
+
obj.cached_script_srcs = None
|
|
335
|
+
obj._selector = None
|
|
336
|
+
return obj
|
|
337
|
+
|
|
338
|
+
def clone(self):
|
|
339
|
+
"""Clone the current node.
|
|
340
|
+
|
|
341
|
+
You can use to do temporary modifications without affecting the original HTML tree.
|
|
342
|
+
|
|
343
|
+
It is tied to the current parser instance.
|
|
344
|
+
Gets destroyed when parser instance is destroyed.
|
|
345
|
+
"""
|
|
346
|
+
cdef lxb_html_document_t* cloned_document
|
|
347
|
+
cdef lxb_dom_node_t* cloned_node
|
|
348
|
+
cdef LexborHTMLParser cls
|
|
349
|
+
|
|
350
|
+
with nogil:
|
|
351
|
+
cloned_document = lxb_html_document_create()
|
|
352
|
+
|
|
353
|
+
if cloned_document == NULL:
|
|
354
|
+
raise SelectolaxError("Can't create a new document")
|
|
355
|
+
|
|
356
|
+
cloned_document.ready_state = LXB_HTML_DOCUMENT_READY_STATE_COMPLETE
|
|
357
|
+
|
|
358
|
+
with nogil:
|
|
359
|
+
cloned_node = lxb_dom_document_import_node(
|
|
360
|
+
&cloned_document.dom_document,
|
|
361
|
+
<lxb_dom_node_t *> lxb_dom_document_root(&self.document.dom_document),
|
|
362
|
+
<bint> True
|
|
363
|
+
)
|
|
364
|
+
|
|
365
|
+
if cloned_node == NULL:
|
|
366
|
+
raise SelectolaxError("Can't create a new document")
|
|
367
|
+
|
|
368
|
+
with nogil:
|
|
369
|
+
lxb_dom_node_insert_child(<lxb_dom_node_t * > cloned_document, cloned_node)
|
|
370
|
+
|
|
371
|
+
cls = LexborHTMLParser.from_document(cloned_document, self.raw_html)
|
|
372
|
+
return cls
|
|
373
|
+
|
|
374
|
+
def unwrap_tags(self, list tags, delete_empty = False):
|
|
375
|
+
"""Unwraps specified tags from the HTML tree.
|
|
376
|
+
|
|
377
|
+
Works the same as the ``unwrap`` method, but applied to a list of tags.
|
|
378
|
+
|
|
379
|
+
Parameters
|
|
380
|
+
----------
|
|
381
|
+
tags : list
|
|
382
|
+
List of tags to remove.
|
|
383
|
+
delete_empty : bool
|
|
384
|
+
Whenever to delete empty tags.
|
|
385
|
+
|
|
386
|
+
Examples
|
|
387
|
+
--------
|
|
388
|
+
|
|
389
|
+
>>> tree = LexborHTMLParser("<div><a href="">Hello</a> <i>world</i>!</div>")
|
|
390
|
+
>>> tree.body.unwrap_tags(['i','a'])
|
|
391
|
+
>>> tree.body.html
|
|
392
|
+
'<body><div>Hello world!</div></body>'
|
|
393
|
+
"""
|
|
394
|
+
# faster to check if the document is empty which should determine if we have a root
|
|
395
|
+
if self.document != NULL:
|
|
396
|
+
self.root.unwrap_tags(tags, delete_empty=delete_empty)
|
|
397
|
+
|
|
398
|
+
@property
|
|
399
|
+
def inner_html(self) -> str:
|
|
400
|
+
"""Return HTML representation of the child nodes.
|
|
401
|
+
|
|
402
|
+
Works similar to innerHTML in JavaScript.
|
|
403
|
+
Unlike the `.html` property, does not include the current node.
|
|
404
|
+
Can be used to set HTML as well. See the setter docstring.
|
|
405
|
+
|
|
406
|
+
Returns
|
|
407
|
+
-------
|
|
408
|
+
text : str | None
|
|
409
|
+
"""
|
|
410
|
+
return self.root.inner_html
|
|
411
|
+
|
|
412
|
+
@inner_html.setter
|
|
413
|
+
def inner_html(self, str html):
|
|
414
|
+
"""Set inner HTML to the specified HTML.
|
|
415
|
+
|
|
416
|
+
Replaces existing data inside the node.
|
|
417
|
+
Works similar to innerHTML in JavaScript.
|
|
418
|
+
|
|
419
|
+
Parameters
|
|
420
|
+
----------
|
|
421
|
+
html : str
|
|
422
|
+
|
|
423
|
+
"""
|
|
424
|
+
self.root.inner_html = html
|