forked from apify/crawlee-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlxml_saxonche_parser.py
More file actions
77 lines (62 loc) · 2.77 KB
/
lxml_saxonche_parser.py
File metadata and controls
77 lines (62 loc) · 2.77 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import asyncio
from lxml import html
from pydantic import ValidationError
from saxonche import PySaxonProcessor
from crawlee import Request
from crawlee.crawlers import HttpCrawler, HttpCrawlingContext
async def main() -> None:
crawler = HttpCrawler(
max_request_retries=1,
max_requests_per_crawl=10,
)
# Create Saxon processor once and reuse across requests.
saxon_proc = PySaxonProcessor(license=False)
xpath_proc = saxon_proc.new_xpath_processor()
@crawler.router.default_handler
async def request_handler(context: HttpCrawlingContext) -> None:
context.log.info(f'Processing {context.request.url} ...')
# Parse HTML with lxml.
parsed_html = html.fromstring(await context.http_response.read())
# Convert relative URLs to absolute before extracting links.
parsed_html.make_links_absolute(context.request.url, resolve_base_href=True)
# Convert parsed HTML to XML for Saxon processing.
xml = html.tostring(parsed_html, encoding='unicode', method='xml')
# Parse XML with Saxon.
parsed_xml = saxon_proc.parse_xml(xml_text=xml)
# Set the parsed context for XPath evaluation.
xpath_proc.set_context(xdm_item=parsed_xml)
# Extract data using XPath 2.0 string() function.
data = {
'url': context.request.url,
'title': xpath_proc.evaluate_single('.//title/string()'),
'h1s': [str(h) for h in (xpath_proc.evaluate('//h1/string()') or [])],
'h2s': [str(h) for h in (xpath_proc.evaluate('//h2/string()') or [])],
'h3s': [str(h) for h in (xpath_proc.evaluate('//h3/string()') or [])],
}
await context.push_data(data)
# XPath 2.0 with distinct-values() to get unique links and remove fragments.
links_xpath = """
distinct-values(
for $href in //a/@href[
not(starts-with(., "#"))
and not(starts-with(., "javascript:"))
and not(starts-with(., "mailto:"))
]
return replace($href, "#.*$", "")
)
"""
extracted_requests = []
# Extract links.
for item in xpath_proc.evaluate(links_xpath) or []:
url = item.string_value
try:
request = Request.from_url(url)
except ValidationError as exc:
context.log.warning(f'Skipping invalid URL "{url}": {exc}')
continue
extracted_requests.append(request)
# Add extracted requests to the queue with the same-domain strategy.
await context.add_requests(extracted_requests, strategy='same-domain')
await crawler.run(['https://crawlee.dev'])
if __name__ == '__main__':
asyncio.run(main())