forked from apify/crawlee-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrapling_parser.py
More file actions
74 lines (60 loc) · 2.46 KB
/
scrapling_parser.py
File metadata and controls
74 lines (60 loc) · 2.46 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
import asyncio
from pydantic import ValidationError
from scrapling.parser import Selector
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 Scrapling.
page = Selector(await context.http_response.read(), url=context.request.url)
# Extract data using Xpath selectors with .get_all_text method for full text
# content.
title_el = page.xpath_first('//title')
data = {
'url': context.request.url,
'title': title_el.text if isinstance(title_el, Selector) else title_el,
'h1s': [
h1.get_all_text() if isinstance(h1, Selector) else h1
for h1 in page.xpath('//h1')
],
'h2s': [
h2.get_all_text() if isinstance(h2, Selector) else h2
for h2 in page.xpath('//h2')
],
'h3s': [
h3.get_all_text() if isinstance(h3, Selector) else h3
for h3 in page.xpath('//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 page.css(links_selector):
href = item.attrib.get('href') if isinstance(item, Selector) else None
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())