-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy path07_scrapling_browser.py
More file actions
35 lines (29 loc) · 1015 Bytes
/
Copy path07_scrapling_browser.py
File metadata and controls
35 lines (29 loc) · 1015 Bytes
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
from typing import Any
from scrapling.fetchers import DynamicFetcher
async def scrape_page(
url: str,
*,
proxy_url: str | None = None,
) -> tuple[dict[str, Any], list[str]]:
"""Fetch a page in a real browser with Scrapling and return data and links."""
# `network_idle` waits until the page stops making network requests.
response = await DynamicFetcher.async_fetch(
url,
proxy=proxy_url,
headless=True,
network_idle=True,
)
data = {
'url': url,
'title': response.css('title::text').get(),
'h1s': response.css('h1::text').getall(),
'h2s': response.css('h2::text').getall(),
'h3s': response.css('h3::text').getall(),
}
# Collect absolute links from the page.
links: list[str] = []
for href in response.css('a::attr(href)').getall():
link_url = response.urljoin(href)
if link_url.startswith(('http://', 'https://')):
links.append(link_url)
return data, links