selectolax 0.3.23__cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.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 +10 -0
- selectolax/base.pxi +4 -0
- selectolax/lexbor/attrs.pxi +102 -0
- selectolax/lexbor/node.pxi +867 -0
- selectolax/lexbor/selection.pxi +182 -0
- selectolax/lexbor.c +49688 -0
- selectolax/lexbor.cpython-313-i386-linux-gnu.so +0 -0
- selectolax/lexbor.pxd +580 -0
- selectolax/lexbor.pyi +122 -0
- selectolax/lexbor.pyx +346 -0
- selectolax/modest/node.pxi +969 -0
- selectolax/modest/selection.pxi +193 -0
- selectolax/parser.c +46799 -0
- selectolax/parser.cpython-313-i386-linux-gnu.so +0 -0
- selectolax/parser.pxd +581 -0
- selectolax/parser.pyi +278 -0
- selectolax/parser.pyx +432 -0
- selectolax/py.typed +0 -0
- selectolax/utils.pxi +13 -0
- selectolax-0.3.23.dist-info/LICENSE +10 -0
- selectolax-0.3.23.dist-info/METADATA +172 -0
- selectolax-0.3.23.dist-info/RECORD +24 -0
- selectolax-0.3.23.dist-info/WHEEL +8 -0
- selectolax-0.3.23.dist-info/top_level.txt +1 -0
selectolax/parser.pyi
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
from typing import Iterator, TypeVar, Literal
|
|
2
|
+
|
|
3
|
+
DefaultT = TypeVar("DefaultT")
|
|
4
|
+
|
|
5
|
+
class _Attributes:
|
|
6
|
+
@staticmethod
|
|
7
|
+
def create(node: "Node", decode_errors: str) -> "_Attributes": ...
|
|
8
|
+
def keys(self) -> Iterator[str]: ...
|
|
9
|
+
def items(self) -> Iterator[tuple[str, str]]: ...
|
|
10
|
+
def values(self) -> Iterator[str]: ...
|
|
11
|
+
def get(self, key, default: DefaultT | None = None) -> str | DefaultT: ...
|
|
12
|
+
def sget(self, key, default: str = "") -> str | DefaultT: ...
|
|
13
|
+
|
|
14
|
+
class Selector:
|
|
15
|
+
"""An advanced CSS selector that supports additional operations.
|
|
16
|
+
|
|
17
|
+
Think of it as a toolkit that mimics some of the features of XPath.
|
|
18
|
+
|
|
19
|
+
Please note, this is an experimental feature that can change in the future."""
|
|
20
|
+
|
|
21
|
+
def __init__(self, node: "Node", query: str): ...
|
|
22
|
+
def css(self, query: str) -> "Node":
|
|
23
|
+
"""Evaluate CSS selector against current scope."""
|
|
24
|
+
...
|
|
25
|
+
@property
|
|
26
|
+
def matches(self) -> list["Node"]:
|
|
27
|
+
"""Returns all possible selector matches"""
|
|
28
|
+
...
|
|
29
|
+
@property
|
|
30
|
+
def any_matches(self) -> bool:
|
|
31
|
+
"""Returns True if there are any matches"""
|
|
32
|
+
...
|
|
33
|
+
def text_contains(
|
|
34
|
+
self, text: str, deep: bool = True, separator: str = "", strip: bool = False
|
|
35
|
+
) -> "Selector":
|
|
36
|
+
"""Filter all current matches given text."""
|
|
37
|
+
...
|
|
38
|
+
def any_text_contains(
|
|
39
|
+
self, text: str, deep: bool = True, separator: str = "", strip: bool = False
|
|
40
|
+
) -> bool:
|
|
41
|
+
"""Returns True if any node in the current search scope contains specified text"""
|
|
42
|
+
...
|
|
43
|
+
def attribute_long_than(
|
|
44
|
+
self, text: str, length: int, start: str | None = None
|
|
45
|
+
) -> "Selector":
|
|
46
|
+
"""Filter all current matches by attribute length.
|
|
47
|
+
|
|
48
|
+
Similar to string-length in XPath."""
|
|
49
|
+
...
|
|
50
|
+
def any_attribute_long_than(
|
|
51
|
+
self, text: str, length: int, start: str | None = None
|
|
52
|
+
) -> bool:
|
|
53
|
+
"""Returns True any href attribute longer than a specified length.
|
|
54
|
+
|
|
55
|
+
Similar to string-length in XPath."""
|
|
56
|
+
...
|
|
57
|
+
|
|
58
|
+
class Node:
|
|
59
|
+
@property
|
|
60
|
+
def attributes(self) -> dict[str, None | str]:
|
|
61
|
+
"""Get all attributes that belong to the current node.
|
|
62
|
+
|
|
63
|
+
The value of empty attributes is None."""
|
|
64
|
+
...
|
|
65
|
+
@property
|
|
66
|
+
def attrs(self) -> "_Attributes":
|
|
67
|
+
"""A dict-like object that is similar to the attributes property, but operates directly on the Node data."""
|
|
68
|
+
...
|
|
69
|
+
@property
|
|
70
|
+
def id(self) -> str | None:
|
|
71
|
+
"""Get the id attribute of the node.
|
|
72
|
+
|
|
73
|
+
Returns None if id does not set."""
|
|
74
|
+
...
|
|
75
|
+
|
|
76
|
+
def mem_id(self) -> int:
|
|
77
|
+
"""Get the mem_id of the node.
|
|
78
|
+
|
|
79
|
+
Returns 0 if mem_id does not set."""
|
|
80
|
+
...
|
|
81
|
+
|
|
82
|
+
def __hash__(self) -> int:
|
|
83
|
+
""" Get the hash of this node
|
|
84
|
+
:return: int
|
|
85
|
+
"""
|
|
86
|
+
...
|
|
87
|
+
def text(self, deep: bool = True, separator: str = "", strip: bool = False) -> str:
|
|
88
|
+
"""Returns the text of the node including text of all its child nodes."""
|
|
89
|
+
...
|
|
90
|
+
def iter(self, include_text: bool = False) -> Iterator["Node"]:
|
|
91
|
+
"""Iterate over nodes on the current level."""
|
|
92
|
+
...
|
|
93
|
+
def traverse(self, include_text: bool = False) -> Iterator["Node"]:
|
|
94
|
+
"""Iterate over all child and next nodes starting from the current level."""
|
|
95
|
+
...
|
|
96
|
+
@property
|
|
97
|
+
def tag(self) -> str:
|
|
98
|
+
"""Return the name of the current tag (e.g. div, p, img)."""
|
|
99
|
+
...
|
|
100
|
+
@property
|
|
101
|
+
def child(self) -> None | "Node":
|
|
102
|
+
"""Return the child node."""
|
|
103
|
+
...
|
|
104
|
+
@property
|
|
105
|
+
def parent(self) -> None | "Node":
|
|
106
|
+
"""Return the parent node."""
|
|
107
|
+
...
|
|
108
|
+
@property
|
|
109
|
+
def next(self) -> None | "Node":
|
|
110
|
+
"""Return next node."""
|
|
111
|
+
...
|
|
112
|
+
@property
|
|
113
|
+
def prev(self) -> None | "Node":
|
|
114
|
+
"""Return previous node."""
|
|
115
|
+
...
|
|
116
|
+
@property
|
|
117
|
+
def last_child(self) -> None | "Node":
|
|
118
|
+
"""Return last child node."""
|
|
119
|
+
...
|
|
120
|
+
@property
|
|
121
|
+
def html(self) -> None | str:
|
|
122
|
+
"""Return HTML representation of the current node including all its child nodes."""
|
|
123
|
+
...
|
|
124
|
+
def css(self, query: str) -> list["Node"]:
|
|
125
|
+
"""Evaluate CSS selector against current node and its child nodes."""
|
|
126
|
+
...
|
|
127
|
+
def any_css_matches(self, selectors: tuple[str]) -> bool:
|
|
128
|
+
"""Returns True if any of CSS selectors matches a node"""
|
|
129
|
+
...
|
|
130
|
+
def css_matches(self, selector: str) -> bool:
|
|
131
|
+
"""Returns True if CSS selector matches a node."""
|
|
132
|
+
...
|
|
133
|
+
def css_first(
|
|
134
|
+
self, query: str, default: DefaultT | None = None, strict: bool = False
|
|
135
|
+
) -> "Node" | DefaultT:
|
|
136
|
+
"""Evaluate CSS selector against current node and its child nodes."""
|
|
137
|
+
...
|
|
138
|
+
def decompose(self, recursive: bool = True) -> None:
|
|
139
|
+
"""Remove a Node from the tree."""
|
|
140
|
+
...
|
|
141
|
+
def remove(self, recursive: bool = True) -> None:
|
|
142
|
+
"""An alias for the decompose method."""
|
|
143
|
+
...
|
|
144
|
+
def unwrap(self) -> None:
|
|
145
|
+
"""Replace node with whatever is inside this node."""
|
|
146
|
+
...
|
|
147
|
+
def strip_tags(self, tags: list[str], recursive: bool = False) -> None:
|
|
148
|
+
"""Remove specified tags from the HTML tree."""
|
|
149
|
+
...
|
|
150
|
+
def unwrap_tags(self, tags: list[str]) -> None:
|
|
151
|
+
"""Unwraps specified tags from the HTML tree.
|
|
152
|
+
|
|
153
|
+
Works the same as the unwrap method, but applied to a list of tags."""
|
|
154
|
+
...
|
|
155
|
+
def replace_with(self, value: str | bytes | None) -> None:
|
|
156
|
+
"""Replace current Node with specified value."""
|
|
157
|
+
...
|
|
158
|
+
def insert_before(self, value: str | bytes | None) -> None:
|
|
159
|
+
"""Insert a node before the current Node."""
|
|
160
|
+
...
|
|
161
|
+
def insert_after(self, value: str | bytes | None) -> None:
|
|
162
|
+
"""Insert a node after the current Node."""
|
|
163
|
+
...
|
|
164
|
+
@property
|
|
165
|
+
def raw_value(self) -> bytes:
|
|
166
|
+
"""Return the raw (unparsed, original) value of a node.
|
|
167
|
+
|
|
168
|
+
Currently, works on text nodes only."""
|
|
169
|
+
...
|
|
170
|
+
def select(self, query: str | None = None) -> "Selector":
|
|
171
|
+
"""Select nodes given a CSS selector.
|
|
172
|
+
|
|
173
|
+
Works similarly to the css method, but supports chained filtering and extra features.
|
|
174
|
+
"""
|
|
175
|
+
...
|
|
176
|
+
def scripts_contain(self, query: str) -> bool:
|
|
177
|
+
"""Returns True if any of the script tags contain specified text.
|
|
178
|
+
|
|
179
|
+
Caches script tags on the first call to improve performance."""
|
|
180
|
+
...
|
|
181
|
+
def script_srcs_contain(self, queries: tuple[str]) -> bool:
|
|
182
|
+
"""Returns True if any of the script SRCs attributes contain on of the specified text.
|
|
183
|
+
|
|
184
|
+
Caches values on the first call to improve performance."""
|
|
185
|
+
...
|
|
186
|
+
@property
|
|
187
|
+
def text_content(self) -> str | None:
|
|
188
|
+
"""Returns the text of the node if it is a text node.
|
|
189
|
+
|
|
190
|
+
Returns None for other nodes. Unlike the text method, does not include child nodes.
|
|
191
|
+
"""
|
|
192
|
+
...
|
|
193
|
+
def merge_text_nodes(self):
|
|
194
|
+
"""Iterates over all text nodes and merges all text nodes that are close to each other.
|
|
195
|
+
|
|
196
|
+
This is useful for text extraction."""
|
|
197
|
+
...
|
|
198
|
+
|
|
199
|
+
class HTMLParser:
|
|
200
|
+
def __init__(
|
|
201
|
+
self,
|
|
202
|
+
html: bytes | str,
|
|
203
|
+
detect_encoding: bool = True,
|
|
204
|
+
use_meta_tags: bool = True,
|
|
205
|
+
decode_errors: Literal["strict", "ignore", "replace"] = "ignore",
|
|
206
|
+
): ...
|
|
207
|
+
def css(self, query: str) -> list["Node"]:
|
|
208
|
+
"""A CSS selector.
|
|
209
|
+
|
|
210
|
+
Matches pattern query against HTML tree."""
|
|
211
|
+
...
|
|
212
|
+
def css_first(
|
|
213
|
+
self, query: str, default: DefaultT | None = None, strict: bool = False
|
|
214
|
+
) -> DefaultT | "Node":
|
|
215
|
+
"""Same as css but returns only the first match."""
|
|
216
|
+
...
|
|
217
|
+
@property
|
|
218
|
+
def input_encoding(self) -> str:
|
|
219
|
+
"""Return encoding of the HTML document.
|
|
220
|
+
|
|
221
|
+
Returns unknown in case the encoding is not determined."""
|
|
222
|
+
...
|
|
223
|
+
@property
|
|
224
|
+
def root(self) -> "Node" | None:
|
|
225
|
+
"""Returns root node."""
|
|
226
|
+
...
|
|
227
|
+
@property
|
|
228
|
+
def head(self) -> "Node" | None:
|
|
229
|
+
"""Returns head node."""
|
|
230
|
+
...
|
|
231
|
+
@property
|
|
232
|
+
def body(self) -> "Node" | None:
|
|
233
|
+
"""Returns document body."""
|
|
234
|
+
...
|
|
235
|
+
def tags(self, name: str) -> list["Node"]:
|
|
236
|
+
"""Returns a list of tags that match specified name."""
|
|
237
|
+
...
|
|
238
|
+
def text(self, deep: bool = True, separator: str = "", strip: bool = False) -> str:
|
|
239
|
+
"""Returns the text of the node including text of all its child nodes."""
|
|
240
|
+
...
|
|
241
|
+
def strip_tags(self, tags: list[str], recursive: bool = False) -> None: ...
|
|
242
|
+
def unwrap_tags(self, tags: list[str]) -> None:
|
|
243
|
+
"""Unwraps specified tags from the HTML tree.
|
|
244
|
+
|
|
245
|
+
Works the same as th unwrap method, but applied to a list of tags."""
|
|
246
|
+
...
|
|
247
|
+
@property
|
|
248
|
+
def html(self) -> None | str:
|
|
249
|
+
"""Return HTML representation of the page."""
|
|
250
|
+
...
|
|
251
|
+
def select(self, query: str | None = None) -> "Selector" | None:
|
|
252
|
+
"""Select nodes given a CSS selector.
|
|
253
|
+
|
|
254
|
+
Works similarly to the css method, but supports chained filtering and extra features.
|
|
255
|
+
"""
|
|
256
|
+
...
|
|
257
|
+
def any_css_matches(self, selectors: tuple[str]) -> bool:
|
|
258
|
+
"""Returns True if any of the specified CSS selectors matches a node."""
|
|
259
|
+
...
|
|
260
|
+
def scripts_contain(self, query: str) -> bool:
|
|
261
|
+
"""Returns True if any of the script tags contain specified text.
|
|
262
|
+
|
|
263
|
+
Caches script tags on the first call to improve performance."""
|
|
264
|
+
...
|
|
265
|
+
def scripts_srcs_contain(self, queries: tuple[str]) -> bool:
|
|
266
|
+
"""Returns True if any of the script SRCs attributes contain on of the specified text.
|
|
267
|
+
|
|
268
|
+
Caches values on the first call to improve performance."""
|
|
269
|
+
...
|
|
270
|
+
def css_matches(self, selector: str) -> bool: ...
|
|
271
|
+
def clone(self) -> "HTMLParser":
|
|
272
|
+
"""Clone the current tree."""
|
|
273
|
+
...
|
|
274
|
+
def merge_text_nodes(self):
|
|
275
|
+
"""Iterates over all text nodes and merges all text nodes that are close to each other.
|
|
276
|
+
|
|
277
|
+
This is useful for text extraction."""
|
|
278
|
+
...
|
selectolax/parser.pyx
ADDED
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
|
|
2
|
+
from cpython cimport bool
|
|
3
|
+
|
|
4
|
+
include "modest/selection.pxi"
|
|
5
|
+
include "modest/node.pxi"
|
|
6
|
+
include "utils.pxi"
|
|
7
|
+
|
|
8
|
+
cdef class HTMLParser:
|
|
9
|
+
"""The HTML parser.
|
|
10
|
+
|
|
11
|
+
Use this class to parse raw HTML.
|
|
12
|
+
|
|
13
|
+
Parameters
|
|
14
|
+
----------
|
|
15
|
+
|
|
16
|
+
html : str (unicode) or bytes
|
|
17
|
+
detect_encoding : bool, default True
|
|
18
|
+
If `True` and html type is `bytes` then encoding will be detected automatically.
|
|
19
|
+
use_meta_tags : bool, default True
|
|
20
|
+
Whether to use meta tags in encoding detection process.
|
|
21
|
+
decode_errors : str, default 'ignore'
|
|
22
|
+
Same as in builtin's str.decode, i.e 'strict', 'ignore' or 'replace'.
|
|
23
|
+
"""
|
|
24
|
+
def __init__(self, html, detect_encoding=True, use_meta_tags=True, decode_errors = 'ignore'):
|
|
25
|
+
|
|
26
|
+
cdef size_t html_len
|
|
27
|
+
cdef char* html_chars
|
|
28
|
+
|
|
29
|
+
self.detect_encoding = detect_encoding
|
|
30
|
+
self.use_meta_tags = use_meta_tags
|
|
31
|
+
self.decode_errors = decode_errors
|
|
32
|
+
self._encoding = MyENCODING_UTF_8
|
|
33
|
+
|
|
34
|
+
bytes_html, html_len = preprocess_input(html, decode_errors)
|
|
35
|
+
html_chars = <char*> bytes_html
|
|
36
|
+
|
|
37
|
+
if detect_encoding and isinstance(html, bytes):
|
|
38
|
+
self._detect_encoding(html_chars, html_len)
|
|
39
|
+
|
|
40
|
+
self._parse_html(html_chars, html_len)
|
|
41
|
+
|
|
42
|
+
self.raw_html = bytes_html
|
|
43
|
+
self.cached_script_texts = None
|
|
44
|
+
self.cached_script_srcs = None
|
|
45
|
+
|
|
46
|
+
def css(self, str query):
|
|
47
|
+
"""A CSS selector.
|
|
48
|
+
|
|
49
|
+
Matches pattern `query` against HTML tree.
|
|
50
|
+
`CSS selectors reference <https://www.w3schools.com/cssref/css_selectors.asp>`_.
|
|
51
|
+
|
|
52
|
+
Parameters
|
|
53
|
+
----------
|
|
54
|
+
query : str
|
|
55
|
+
CSS selector (e.g. "div > :nth-child(2n+1):not(:has(a))").
|
|
56
|
+
|
|
57
|
+
Returns
|
|
58
|
+
-------
|
|
59
|
+
selector : list of `Node` objects
|
|
60
|
+
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
node = Node()
|
|
64
|
+
node._init(self.html_tree.node_html, self)
|
|
65
|
+
return node.css(query)
|
|
66
|
+
|
|
67
|
+
def css_first(self, str query, default=None, strict=False):
|
|
68
|
+
"""Same as `css` but returns only the first match.
|
|
69
|
+
|
|
70
|
+
Parameters
|
|
71
|
+
----------
|
|
72
|
+
|
|
73
|
+
query : str
|
|
74
|
+
default : bool, default None
|
|
75
|
+
Default value to return if there is no match.
|
|
76
|
+
strict: bool, default True
|
|
77
|
+
Set to True if you want to check if there is strictly only one match in the document.
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
Returns
|
|
81
|
+
-------
|
|
82
|
+
selector : `Node` object
|
|
83
|
+
|
|
84
|
+
"""
|
|
85
|
+
|
|
86
|
+
node = Node()
|
|
87
|
+
node._init(self.html_tree.node_html, self)
|
|
88
|
+
return node.css_first(query, default, strict)
|
|
89
|
+
|
|
90
|
+
cdef void _detect_encoding(self, char* html, size_t html_len) nogil:
|
|
91
|
+
cdef myencoding_t encoding = MyENCODING_DEFAULT;
|
|
92
|
+
|
|
93
|
+
if self.use_meta_tags:
|
|
94
|
+
encoding = myencoding_prescan_stream_to_determine_encoding(html, html_len)
|
|
95
|
+
if encoding != MyENCODING_DEFAULT and encoding != MyENCODING_NOT_DETERMINED:
|
|
96
|
+
self._encoding = encoding
|
|
97
|
+
return
|
|
98
|
+
|
|
99
|
+
if not myencoding_detect_bom(html, html_len, &encoding):
|
|
100
|
+
myencoding_detect(html, html_len, &encoding)
|
|
101
|
+
|
|
102
|
+
self._encoding = encoding
|
|
103
|
+
|
|
104
|
+
cdef _parse_html(self, char* html, size_t html_len):
|
|
105
|
+
cdef myhtml_t* myhtml
|
|
106
|
+
cdef mystatus_t status
|
|
107
|
+
|
|
108
|
+
with nogil:
|
|
109
|
+
myhtml = myhtml_create()
|
|
110
|
+
status = myhtml_init(myhtml, MyHTML_OPTIONS_DEFAULT, 1, 0)
|
|
111
|
+
|
|
112
|
+
if status != 0:
|
|
113
|
+
raise RuntimeError("Can't init MyHTML object.")
|
|
114
|
+
|
|
115
|
+
with nogil:
|
|
116
|
+
self.html_tree = myhtml_tree_create()
|
|
117
|
+
status = myhtml_tree_init(self.html_tree, myhtml)
|
|
118
|
+
|
|
119
|
+
if status != 0:
|
|
120
|
+
raise RuntimeError("Can't init MyHTML Tree object.")
|
|
121
|
+
|
|
122
|
+
with nogil:
|
|
123
|
+
status = myhtml_parse(self.html_tree, self._encoding, html, html_len)
|
|
124
|
+
|
|
125
|
+
if status != 0:
|
|
126
|
+
raise RuntimeError("Can't parse HTML:\n%s" % str(html))
|
|
127
|
+
|
|
128
|
+
assert self.html_tree.node_html != NULL
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
@property
|
|
132
|
+
def input_encoding(self):
|
|
133
|
+
"""Return encoding of the HTML document.
|
|
134
|
+
|
|
135
|
+
Returns `unknown` in case the encoding is not determined.
|
|
136
|
+
"""
|
|
137
|
+
cdef const char* encoding
|
|
138
|
+
encoding = myencoding_name_by_id(self._encoding, NULL)
|
|
139
|
+
|
|
140
|
+
if encoding != NULL:
|
|
141
|
+
return encoding.decode('utf-8')
|
|
142
|
+
else:
|
|
143
|
+
return 'unknown'
|
|
144
|
+
|
|
145
|
+
@property
|
|
146
|
+
def root(self):
|
|
147
|
+
"""Returns root node."""
|
|
148
|
+
if self.html_tree and self.html_tree.node_html:
|
|
149
|
+
node = Node()
|
|
150
|
+
node._init(self.html_tree.node_html, self)
|
|
151
|
+
return node
|
|
152
|
+
return None
|
|
153
|
+
|
|
154
|
+
@property
|
|
155
|
+
def head(self):
|
|
156
|
+
"""Returns head node."""
|
|
157
|
+
cdef myhtml_tree_node_t* head
|
|
158
|
+
head = myhtml_tree_get_node_head(self.html_tree)
|
|
159
|
+
|
|
160
|
+
if head != NULL:
|
|
161
|
+
node = Node()
|
|
162
|
+
node._init(head, self)
|
|
163
|
+
return node
|
|
164
|
+
return None
|
|
165
|
+
|
|
166
|
+
@property
|
|
167
|
+
def body(self):
|
|
168
|
+
"""Returns document body."""
|
|
169
|
+
cdef myhtml_tree_node_t* body
|
|
170
|
+
body = myhtml_tree_get_node_body(self.html_tree)
|
|
171
|
+
|
|
172
|
+
if body != NULL:
|
|
173
|
+
node = Node()
|
|
174
|
+
node._init(body, self)
|
|
175
|
+
return node
|
|
176
|
+
|
|
177
|
+
return None
|
|
178
|
+
|
|
179
|
+
def tags(self, str name):
|
|
180
|
+
"""Returns a list of tags that match specified name.
|
|
181
|
+
|
|
182
|
+
Parameters
|
|
183
|
+
----------
|
|
184
|
+
name : str (e.g. div)
|
|
185
|
+
|
|
186
|
+
"""
|
|
187
|
+
cdef myhtml_collection_t* collection = NULL
|
|
188
|
+
pybyte_name = name.encode('UTF-8')
|
|
189
|
+
cdef mystatus_t status = 0;
|
|
190
|
+
|
|
191
|
+
result = list()
|
|
192
|
+
collection = myhtml_get_nodes_by_name(self.html_tree, NULL, pybyte_name, len(pybyte_name), &status)
|
|
193
|
+
|
|
194
|
+
if collection == NULL:
|
|
195
|
+
return result
|
|
196
|
+
|
|
197
|
+
if status == 0:
|
|
198
|
+
for i in range(collection.length):
|
|
199
|
+
node = Node()
|
|
200
|
+
node._init(collection.list[i], self)
|
|
201
|
+
result.append(node)
|
|
202
|
+
|
|
203
|
+
myhtml_collection_destroy(collection)
|
|
204
|
+
|
|
205
|
+
return result
|
|
206
|
+
|
|
207
|
+
def text(self, bool deep=True, str separator='', bool strip=False):
|
|
208
|
+
"""Returns the text of the node including text of all its child nodes.
|
|
209
|
+
|
|
210
|
+
Parameters
|
|
211
|
+
----------
|
|
212
|
+
strip : bool, default False
|
|
213
|
+
If true, calls ``str.strip()`` on each text part to remove extra white spaces.
|
|
214
|
+
separator : str, default ''
|
|
215
|
+
The separator to use when joining text from different nodes.
|
|
216
|
+
deep : bool, default True
|
|
217
|
+
If True, includes text from all child nodes.
|
|
218
|
+
|
|
219
|
+
Returns
|
|
220
|
+
-------
|
|
221
|
+
text : str
|
|
222
|
+
|
|
223
|
+
"""
|
|
224
|
+
if not self.body:
|
|
225
|
+
return ""
|
|
226
|
+
return self.body.text(deep=deep, separator=separator, strip=strip)
|
|
227
|
+
|
|
228
|
+
def strip_tags(self, list tags, bool recursive = False):
|
|
229
|
+
"""Remove specified tags from the node.
|
|
230
|
+
|
|
231
|
+
Parameters
|
|
232
|
+
----------
|
|
233
|
+
tags : list of str
|
|
234
|
+
List of tags to remove.
|
|
235
|
+
recursive : bool, default True
|
|
236
|
+
Whenever to delete all its child nodes
|
|
237
|
+
|
|
238
|
+
Examples
|
|
239
|
+
--------
|
|
240
|
+
|
|
241
|
+
>>> tree = HTMLParser('<html><head></head><body><script></script><div>Hello world!</div></body></html>')
|
|
242
|
+
>>> tags = ['head', 'style', 'script', 'xmp', 'iframe', 'noembed', 'noframes']
|
|
243
|
+
>>> tree.strip_tags(tags)
|
|
244
|
+
>>> tree.html
|
|
245
|
+
'<html><body><div>Hello world!</div></body></html>'
|
|
246
|
+
|
|
247
|
+
"""
|
|
248
|
+
cdef myhtml_collection_t* collection = NULL
|
|
249
|
+
|
|
250
|
+
cdef mystatus_t status = 0;
|
|
251
|
+
|
|
252
|
+
for tag in tags:
|
|
253
|
+
pybyte_name = tag.encode('UTF-8')
|
|
254
|
+
collection = myhtml_get_nodes_by_name(self.html_tree, NULL, pybyte_name, len(pybyte_name), &status)
|
|
255
|
+
|
|
256
|
+
if collection == NULL:
|
|
257
|
+
continue
|
|
258
|
+
|
|
259
|
+
if status != 0:
|
|
260
|
+
continue
|
|
261
|
+
|
|
262
|
+
for i in range(collection.length):
|
|
263
|
+
if recursive:
|
|
264
|
+
myhtml_node_delete_recursive(collection.list[i])
|
|
265
|
+
else:
|
|
266
|
+
myhtml_node_delete(collection.list[i])
|
|
267
|
+
|
|
268
|
+
myhtml_collection_destroy(collection)
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def unwrap_tags(self, list tags):
|
|
272
|
+
"""Unwraps specified tags from the HTML tree.
|
|
273
|
+
|
|
274
|
+
Works the same as th `unwrap` method, but applied to a list of tags.
|
|
275
|
+
|
|
276
|
+
Parameters
|
|
277
|
+
----------
|
|
278
|
+
tags : list
|
|
279
|
+
List of tags to remove.
|
|
280
|
+
|
|
281
|
+
Examples
|
|
282
|
+
--------
|
|
283
|
+
|
|
284
|
+
>>> tree = HTMLParser("<div><a href="">Hello</a> <i>world</i>!</div>")
|
|
285
|
+
>>> tree.head.unwrap_tags(['i','a'])
|
|
286
|
+
>>> tree.head.html
|
|
287
|
+
'<body><div>Hello world!</div></body>'
|
|
288
|
+
"""
|
|
289
|
+
if self.root is not None:
|
|
290
|
+
self.root.unwrap_tags(tags)
|
|
291
|
+
|
|
292
|
+
@property
|
|
293
|
+
def html(self):
|
|
294
|
+
"""Return HTML representation of the page."""
|
|
295
|
+
if self.html_tree and self.html_tree.document:
|
|
296
|
+
node = Node()
|
|
297
|
+
node._init(self.html_tree.document, self)
|
|
298
|
+
return node.html
|
|
299
|
+
return None
|
|
300
|
+
|
|
301
|
+
def select(self, query=None):
|
|
302
|
+
"""Select nodes given a CSS selector.
|
|
303
|
+
|
|
304
|
+
Works similarly to the ``css`` method, but supports chained filtering and extra features.
|
|
305
|
+
|
|
306
|
+
Parameters
|
|
307
|
+
----------
|
|
308
|
+
query : str or None
|
|
309
|
+
The CSS selector to use when searching for nodes.
|
|
310
|
+
|
|
311
|
+
Returns
|
|
312
|
+
-------
|
|
313
|
+
selector : The `Selector` class.
|
|
314
|
+
"""
|
|
315
|
+
cdef Node node
|
|
316
|
+
node = self.root
|
|
317
|
+
if node:
|
|
318
|
+
return Selector(node, query)
|
|
319
|
+
|
|
320
|
+
def any_css_matches(self, tuple selectors):
|
|
321
|
+
"""Returns True if any of the specified CSS selectors matches a node."""
|
|
322
|
+
return self.root.any_css_matches(selectors)
|
|
323
|
+
|
|
324
|
+
def scripts_contain(self, str query):
|
|
325
|
+
"""Returns True if any of the script tags contain specified text.
|
|
326
|
+
|
|
327
|
+
Caches script tags on the first call to improve performance.
|
|
328
|
+
|
|
329
|
+
Parameters
|
|
330
|
+
----------
|
|
331
|
+
query : str
|
|
332
|
+
The query to check.
|
|
333
|
+
|
|
334
|
+
"""
|
|
335
|
+
return self.root.scripts_contain(query)
|
|
336
|
+
|
|
337
|
+
def script_srcs_contain(self, tuple queries):
|
|
338
|
+
"""Returns True if any of the script SRCs attributes contain on of the specified text.
|
|
339
|
+
|
|
340
|
+
Caches values on the first call to improve performance.
|
|
341
|
+
|
|
342
|
+
Parameters
|
|
343
|
+
----------
|
|
344
|
+
queries : tuple of str
|
|
345
|
+
|
|
346
|
+
"""
|
|
347
|
+
return self.root.script_srcs_contain(queries)
|
|
348
|
+
|
|
349
|
+
def css_matches(self, str selector):
|
|
350
|
+
return self.root.css_matches(selector)
|
|
351
|
+
def merge_text_nodes(self):
|
|
352
|
+
"""Iterates over all text nodes and merges all text nodes that are close to each other.
|
|
353
|
+
|
|
354
|
+
This is useful for text extraction.
|
|
355
|
+
Use it when you need to strip HTML tags and merge "dangling" text.
|
|
356
|
+
|
|
357
|
+
Examples
|
|
358
|
+
--------
|
|
359
|
+
|
|
360
|
+
>>> tree = HTMLParser("<div><p><strong>J</strong>ohn</p><p>Doe</p></div>")
|
|
361
|
+
>>> node = tree.css_first('div')
|
|
362
|
+
>>> tree.unwrap_tags(["strong"])
|
|
363
|
+
>>> tree.text(deep=True, separator=" ", strip=True)
|
|
364
|
+
"J ohn Doe" # Text extraction produces an extra space because the strong tag was removed.
|
|
365
|
+
>>> node.merge_text_nodes()
|
|
366
|
+
>>> tree.text(deep=True, separator=" ", strip=True)
|
|
367
|
+
"John Doe"
|
|
368
|
+
"""
|
|
369
|
+
return self.root.merge_text_nodes()
|
|
370
|
+
@staticmethod
|
|
371
|
+
cdef HTMLParser from_tree(
|
|
372
|
+
myhtml_tree_t * tree, bytes raw_html, bint detect_encoding, bint use_meta_tags, str decode_errors,
|
|
373
|
+
myencoding_t encoding
|
|
374
|
+
):
|
|
375
|
+
obj = <HTMLParser> HTMLParser.__new__(HTMLParser)
|
|
376
|
+
obj.html_tree = tree
|
|
377
|
+
obj.raw_html = raw_html
|
|
378
|
+
obj.detect_encoding = detect_encoding
|
|
379
|
+
obj.use_meta_tags = use_meta_tags
|
|
380
|
+
obj.decode_errors = decode_errors
|
|
381
|
+
obj._encoding = encoding
|
|
382
|
+
obj.cached_script_texts = None
|
|
383
|
+
obj.cached_script_srcs = None
|
|
384
|
+
return obj
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
def clone(self):
|
|
388
|
+
"""Clone the current tree."""
|
|
389
|
+
cdef myhtml_t* myhtml
|
|
390
|
+
cdef mystatus_t status
|
|
391
|
+
cdef myhtml_tree_t* html_tree
|
|
392
|
+
cdef myhtml_tree_node_t* node
|
|
393
|
+
|
|
394
|
+
with nogil:
|
|
395
|
+
myhtml = myhtml_create()
|
|
396
|
+
status = myhtml_init(myhtml, MyHTML_OPTIONS_DEFAULT, 1, 0)
|
|
397
|
+
|
|
398
|
+
if status != 0:
|
|
399
|
+
raise RuntimeError("Can't init MyHTML object.")
|
|
400
|
+
|
|
401
|
+
with nogil:
|
|
402
|
+
html_tree = myhtml_tree_create()
|
|
403
|
+
status = myhtml_tree_init(html_tree, myhtml)
|
|
404
|
+
|
|
405
|
+
if status != 0:
|
|
406
|
+
raise RuntimeError("Can't init MyHTML Tree object.")
|
|
407
|
+
|
|
408
|
+
node = myhtml_node_clone_deep(html_tree, self.html_tree.node_html)
|
|
409
|
+
myhtml_tree_node_add_child(html_tree.document, node)
|
|
410
|
+
html_tree.node_html = node
|
|
411
|
+
|
|
412
|
+
cls = HTMLParser.from_tree(
|
|
413
|
+
html_tree,
|
|
414
|
+
self.raw_html,
|
|
415
|
+
self.detect_encoding,
|
|
416
|
+
self.use_meta_tags,
|
|
417
|
+
self.decode_errors,
|
|
418
|
+
self._encoding
|
|
419
|
+
)
|
|
420
|
+
return cls
|
|
421
|
+
|
|
422
|
+
def __dealloc__(self):
|
|
423
|
+
cdef myhtml_t* myhtml
|
|
424
|
+
|
|
425
|
+
if self.html_tree != NULL:
|
|
426
|
+
myhtml = self.html_tree.myhtml
|
|
427
|
+
myhtml_tree_destroy(self.html_tree)
|
|
428
|
+
if myhtml != NULL:
|
|
429
|
+
myhtml_destroy(myhtml)
|
|
430
|
+
|
|
431
|
+
def __repr__(self):
|
|
432
|
+
return '<HTMLParser chars=%s>' % len(self.root.html)
|