-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathhandler.py
390 lines (327 loc) · 16.3 KB
/
handler.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import asyncio
import logging
import warnings
from collections import defaultdict
from contextlib import suppress
from inspect import isawaitable
from ipaddress import ip_address
from time import time
from typing import Callable, Dict, Generator, Optional, Tuple, Type, TypeVar
from playwright.async_api import (
BrowserContext,
Page,
PlaywrightContextManager,
Request as PlaywrightRequest,
Response as PlaywrightResponse,
Route,
)
from scrapy import Spider, signals
from scrapy.core.downloader.handlers.http import HTTPDownloadHandler
from scrapy.crawler import Crawler
from scrapy.exceptions import ScrapyDeprecationWarning
from scrapy.http import Request, Response
from scrapy.http.headers import Headers
from scrapy.responsetypes import responsetypes
from scrapy.utils.defer import deferred_from_coro
from scrapy.utils.misc import load_object
from scrapy.utils.python import to_unicode
from scrapy.utils.reactor import verify_installed_reactor
from twisted.internet.defer import Deferred, inlineCallbacks
from w3lib.encoding import html_body_declared_encoding, http_content_type_encoding
from scrapy_playwright.headers import use_scrapy_headers
from scrapy_playwright.page import PageMethod
__all__ = ["ScrapyPlaywrightDownloadHandler"]
PlaywrightHandler = TypeVar("PlaywrightHandler", bound="ScrapyPlaywrightDownloadHandler")
logger = logging.getLogger("scrapy-playwright")
def _make_request_logger(context_name: str) -> Callable:
def _log_request(request: PlaywrightRequest) -> None:
logger.debug(
f"[Context={context_name}] Request: <{request.method.upper()} {request.url}> "
f"(resource type: {request.resource_type}, referrer: {request.headers.get('referer')})"
)
return _log_request
def _make_response_logger(context_name: str) -> Callable:
def _log_request(response: PlaywrightResponse) -> None:
logger.debug(
f"[Context={context_name}] Response: <{response.status} {response.url}> "
f"(referrer: {response.headers.get('referer')})"
)
return _log_request
class ScrapyPlaywrightDownloadHandler(HTTPDownloadHandler):
def __init__(self, crawler: Crawler) -> None:
super().__init__(settings=crawler.settings, crawler=crawler)
verify_installed_reactor("twisted.internet.asyncioreactor.AsyncioSelectorReactor")
crawler.signals.connect(self._engine_started, signals.engine_started)
self.stats = crawler.stats
self.browser_type: str = crawler.settings.get("PLAYWRIGHT_BROWSER_TYPE") or "chromium"
self.max_pages_per_context: int = crawler.settings.getint(
"PLAYWRIGHT_MAX_PAGES_PER_CONTEXT"
) or crawler.settings.getint("CONCURRENT_REQUESTS")
self.launch_options: dict = crawler.settings.getdict("PLAYWRIGHT_LAUNCH_OPTIONS") or {}
self.default_navigation_timeout: Optional[float] = None
if "PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT" in crawler.settings:
with suppress(TypeError, ValueError):
self.default_navigation_timeout = float(
crawler.settings.get("PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT")
)
if crawler.settings.get("PLAYWRIGHT_PROCESS_REQUEST_HEADERS"):
self.process_request_headers = load_object(
crawler.settings["PLAYWRIGHT_PROCESS_REQUEST_HEADERS"]
)
else:
self.process_request_headers = use_scrapy_headers
default_context_kwargs: dict = {}
if "PLAYWRIGHT_CONTEXT_ARGS" in crawler.settings:
default_context_kwargs = crawler.settings.getdict("PLAYWRIGHT_CONTEXT_ARGS")
warnings.warn(
"The PLAYWRIGHT_CONTEXT_ARGS setting is deprecated, please use"
" PLAYWRIGHT_CONTEXTS instead. Keyword arguments defined in"
" PLAYWRIGHT_CONTEXT_ARGS will be used when creating the 'default' context",
category=ScrapyDeprecationWarning,
stacklevel=2,
)
self.context_kwargs: defaultdict = defaultdict(dict)
for name, kwargs in (crawler.settings.getdict("PLAYWRIGHT_CONTEXTS") or {}).items():
if name == "default":
self.context_kwargs[name] = default_context_kwargs
self.context_kwargs[name].update(kwargs)
if "default" not in self.context_kwargs and default_context_kwargs:
self.context_kwargs["default"] = default_context_kwargs
self.contexts: Dict[str, BrowserContext] = {}
self.context_semaphores: Dict[str, asyncio.Semaphore] = {}
self.abort_request: Optional[Callable[[PlaywrightRequest], bool]] = None
if crawler.settings.get("PLAYWRIGHT_ABORT_REQUEST"):
self.abort_request = load_object(crawler.settings["PLAYWRIGHT_ABORT_REQUEST"])
@classmethod
def from_crawler(cls: Type[PlaywrightHandler], crawler: Crawler) -> PlaywrightHandler:
return cls(crawler)
def _engine_started(self) -> Deferred:
"""Launch the browser. Use the engine_started signal as it supports returning deferreds."""
return deferred_from_coro(self._launch_browser())
async def _launch_browser(self) -> None:
self.playwright_context_manager = PlaywrightContextManager()
self.playwright = await self.playwright_context_manager.start()
logger.info("Launching browser")
browser_launcher = getattr(self.playwright, self.browser_type).launch
self.browser = await browser_launcher(**self.launch_options)
logger.info(f"Browser {self.browser_type} launched")
contexts = await asyncio.gather(
*[
self._create_browser_context(name, kwargs)
for name, kwargs in self.context_kwargs.items()
]
)
self.contexts = dict(zip(self.context_kwargs.keys(), contexts))
self.context_semaphores = {
name: asyncio.Semaphore(value=self.max_pages_per_context) for name in self.contexts
}
async def _create_browser_context(self, name: str, context_kwargs: dict) -> BrowserContext:
context = await self.browser.new_context(**context_kwargs)
context.on("close", self._make_close_browser_context_callback(name))
logger.debug("Browser context started: '%s'", name)
self.stats.inc_value("playwright/context_count")
if self.default_navigation_timeout is not None:
context.set_default_navigation_timeout(self.default_navigation_timeout)
return context
async def _create_page(self, request: Request) -> Page:
"""Create a new page in a context, also creating a new context if necessary."""
context_name = request.meta.setdefault("playwright_context", "default")
context = self.contexts.get(context_name)
if context is None:
context_kwargs = request.meta.get("playwright_context_kwargs") or {}
context = await self._create_browser_context(context_name, context_kwargs)
self.contexts[context_name] = context
self.context_semaphores[context_name] = asyncio.Semaphore(
value=self.max_pages_per_context
)
await self.context_semaphores[context_name].acquire()
page = await context.new_page()
self.stats.inc_value("playwright/page_count")
logger.debug(
"[Context=%s] New page created, page count is %i (%i for all contexts)",
context_name,
len(context.pages),
self._get_total_page_count(),
)
if self.default_navigation_timeout is not None:
page.set_default_navigation_timeout(self.default_navigation_timeout)
page.on("close", self._make_close_page_callback(context_name))
page.on("crash", self._make_close_page_callback(context_name))
page.on("request", _make_request_logger(context_name))
page.on("response", _make_response_logger(context_name))
page.on("request", self._increment_request_stats)
page.on("response", self._increment_response_stats)
return page
def _get_total_page_count(self):
count = sum([len(context.pages) for context in self.contexts.values()])
current_max_count = self.stats.get_value("playwright/page_count/max_concurrent")
if current_max_count is None or count > current_max_count:
self.stats.set_value("playwright/page_count/max_concurrent", count)
return count
@inlineCallbacks
def close(self) -> Deferred:
yield super().close()
yield deferred_from_coro(self._close())
async def _close(self) -> None:
self.contexts.clear()
self.context_semaphores.clear()
if getattr(self, "browser", None):
logger.info("Closing browser")
await self.browser.close()
await self.playwright_context_manager.__aexit__()
def download_request(self, request: Request, spider: Spider) -> Deferred:
if request.meta.get("playwright"):
return deferred_from_coro(self._download_request(request, spider))
return super().download_request(request, spider)
async def _download_request(self, request: Request, spider: Spider) -> Response:
page = request.meta.get("playwright_page")
if not isinstance(page, Page):
page = await self._create_page(request)
# attach event handlers
event_handlers = request.meta.get("playwright_page_event_handlers") or {}
for event, handler in event_handlers.items():
if callable(handler):
page.on(event, handler)
elif isinstance(handler, str):
try:
page.on(event, getattr(spider, handler))
except AttributeError:
logger.warning(
f"Spider '{spider.name}' does not have a '{handler}' attribute,"
f" ignoring handler for event '{event}'"
)
await page.unroute("**")
await page.route(
"**",
self._make_request_handler(
method=request.method,
scrapy_headers=request.headers,
body=request.body,
encoding=getattr(request, "encoding", None),
),
)
try:
result = await self._download_request_with_page(request, page)
except Exception:
if not page.is_closed():
await page.close()
self.stats.inc_value("playwright/page_count/closed")
raise
else:
return result
async def _download_request_with_page(self, request: Request, page: Page) -> Response:
start_time = time()
response = await page.goto(request.url)
await self._apply_page_methods(page, request)
body_str = await page.content()
request.meta["download_latency"] = time() - start_time
if request.meta.get("playwright_include_page"):
request.meta["playwright_page"] = page
else:
await page.close()
self.stats.inc_value("playwright/page_count/closed")
server_ip_address = None
with suppress(AttributeError, KeyError, ValueError):
server_addr = await response.server_addr()
server_ip_address = ip_address(server_addr["ipAddress"])
with suppress(AttributeError):
request.meta["playwright_security_details"] = await response.security_details()
headers = Headers(response.headers)
headers.pop("Content-Encoding", None)
body, encoding = _encode_body(headers=headers, text=body_str)
respcls = responsetypes.from_args(headers=headers, url=page.url, body=body)
return respcls(
url=page.url,
status=response.status,
headers=headers,
body=body,
request=request,
flags=["playwright"],
encoding=encoding,
ip_address=server_ip_address,
)
async def _apply_page_methods(self, page: Page, request: Request) -> None:
page_methods = request.meta.get("playwright_page_methods") or ()
if not page_methods and "playwright_page_coroutines" in request.meta:
page_methods = request.meta["playwright_page_coroutines"]
warnings.warn(
"The 'playwright_page_coroutines' request meta key is deprecated,"
" please use 'playwright_page_methods' instead.",
category=ScrapyDeprecationWarning,
stacklevel=1,
)
if isinstance(page_methods, dict):
page_methods = page_methods.values()
for pm in page_methods:
if isinstance(pm, PageMethod):
try:
method = getattr(page, pm.method)
except AttributeError:
logger.warning(f"Ignoring {repr(pm)}: could not find method")
else:
result = method(*pm.args, **pm.kwargs)
pm.result = await result if isawaitable(result) else result
await page.wait_for_load_state(timeout=self.default_navigation_timeout)
else:
logger.warning(f"Ignoring {repr(pm)}: expected PageMethod, got {repr(type(pm))}")
def _increment_request_stats(self, request: PlaywrightRequest) -> None:
stats_prefix = "playwright/request_count"
self.stats.inc_value(stats_prefix)
self.stats.inc_value(f"{stats_prefix}/resource_type/{request.resource_type}")
self.stats.inc_value(f"{stats_prefix}/method/{request.method}")
if request.is_navigation_request():
self.stats.inc_value(f"{stats_prefix}/navigation")
def _increment_response_stats(self, response: PlaywrightResponse) -> None:
stats_prefix = "playwright/response_count"
self.stats.inc_value(stats_prefix)
self.stats.inc_value(f"{stats_prefix}/resource_type/{response.request.resource_type}")
self.stats.inc_value(f"{stats_prefix}/method/{response.request.method}")
def _make_close_page_callback(self, context_name: str) -> Callable:
def close_page_callback() -> None:
if context_name in self.context_semaphores:
self.context_semaphores[context_name].release()
return close_page_callback
def _make_close_browser_context_callback(self, name: str) -> Callable:
def close_browser_context_callback() -> None:
logger.debug("Browser context closed: '%s'", name)
if name in self.contexts:
self.contexts.pop(name)
if name in self.context_semaphores:
self.context_semaphores.pop(name)
return close_browser_context_callback
def _make_request_handler(
self, method: str, scrapy_headers: Headers, body: Optional[bytes], encoding: str = "utf8"
) -> Callable:
async def _request_handler(route: Route, playwright_request: PlaywrightRequest) -> None:
"""Override request headers, method and body."""
if self.abort_request and self.abort_request(playwright_request):
await route.abort()
self.stats.inc_value("playwright/request_count/aborted")
return None
processed_headers = await self.process_request_headers(
self.browser_type, playwright_request, scrapy_headers
)
# the request that reaches the callback should contain the headers that were sent
scrapy_headers.clear()
scrapy_headers.update(processed_headers)
overrides: dict = {"headers": processed_headers}
if playwright_request.is_navigation_request():
overrides["method"] = method
if body is not None:
overrides["post_data"] = body.decode(encoding)
await route.continue_(**overrides)
return _request_handler
def _possible_encodings(headers: Headers, text: str) -> Generator[str, None, None]:
if headers.get("content-type"):
content_type = to_unicode(headers["content-type"])
yield http_content_type_encoding(content_type)
yield html_body_declared_encoding(text)
def _encode_body(headers: Headers, text: str) -> Tuple[bytes, str]:
for encoding in filter(None, _possible_encodings(headers, text)):
try:
body = text.encode(encoding)
except UnicodeEncodeError:
pass
else:
return body, encoding
return text.encode("utf-8"), "utf-8" # fallback