forked from apify/crawlee-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexbor_parser.py
More file actions
63 lines (49 loc) · 2.06 KB
/
lexbor_parser.py
File metadata and controls
63 lines (49 loc) · 2.06 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
import asyncio
from pydantic import ValidationError
from selectolax.lexbor import LexborHTMLParser
from yarl import URL
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,
)
@crawler.router.default_handler
async def request_handler(context: HttpCrawlingContext) -> None:
context.log.info(f'Processing {context.request.url} ...')
# Parse the HTML content using Selectolax with Lexbor backend.
parsed_html = LexborHTMLParser(await context.http_response.read())
# Extract data from the page.
data = {
'url': context.request.url,
'title': parsed_html.css_first('title').text(),
'h1s': [h1.text() for h1 in parsed_html.css('h1')],
'h2s': [h2.text() for h2 in parsed_html.css('h2')],
'h3s': [h3.text() for h3 in parsed_html.css('h3')],
}
await context.push_data(data)
# Css selector to extract valid href attributes.
links_selector = (
'a[href]:not([href^="#"]):not([href^="javascript:"]):not([href^="mailto:"])'
)
base_url = URL(context.request.url)
extracted_requests = []
# Extract links.
for item in parsed_html.css(links_selector):
href = item.attributes.get('href')
if not href:
continue
# Convert relative URLs to absolute if needed.
url = str(base_url.join(URL(href)))
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())