Repository navigation
Expand file tree
/
Copy pathselectolax_parser.py
More file actions
60 lines (47 loc) · 1.99 KB
/
Copy pathselectolax_parser.py
File metadata and controls
60 lines (47 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from __future__ import annotations
import asyncio
from typing import TYPE_CHECKING
from selectolax.lexbor import LexborHTMLParser, LexborNode
from typing_extensions import override
from crawlee.crawlers._abstract_http import AbstractHttpParser
if TYPE_CHECKING:
from collections.abc import Iterable, Sequence
from crawlee.http_clients import HttpResponse
class SelectolaxLexborParser(AbstractHttpParser[LexborHTMLParser, LexborNode]):
"""Parser for parsing HTTP response using Selectolax Lexbor."""
@override
async def parse(self, response: HttpResponse) -> LexborHTMLParser:
"""Parse HTTP response body into a document object."""
response_body = await response.read()
# Run parsing in a thread to avoid blocking the event loop.
return await asyncio.to_thread(LexborHTMLParser, response_body)
@override
async def parse_text(self, text: str) -> LexborHTMLParser:
"""Parse raw HTML string into a document object."""
return LexborHTMLParser(text)
@override
async def select(
self, parsed_content: LexborHTMLParser, selector: str
) -> Sequence[LexborNode]:
"""Select elements matching a CSS selector."""
return tuple(item for item in parsed_content.css(selector))
@override
def is_matching_selector(
self, parsed_content: LexborHTMLParser, selector: str
) -> bool:
"""Check if any element matches the selector."""
return parsed_content.css_first(selector) is not None
@override
def find_links(
self, parsed_content: LexborHTMLParser, selector: str, *, attribute: str
) -> Iterable[str]:
"""Extract href attributes from elements matching the selector.
Used by `enqueue_links` helper to discover URLs.
"""
link: LexborNode
urls: list[str] = []
for link in parsed_content.css(selector):
url = link.attributes.get(attribute)
if url:
urls.append(url.strip())
return urls