Skip to content

Commit fddc577

Browse files
authored
fix(crawlers): Use the HTTP client and session pool of AdaptivePlaywrightCrawler in its sub crawlers (#2451)
### Description - Both sub crawlers of `AdaptivePlaywrightCrawler` use the HTTP client and the session pool of the adaptive crawler, instead of building defaults of their own that nothing entered or closed. - Static requests and robots.txt lookups go through the client that the crawler enters and closes on every run. ### Issues - Part of: #2301 - Follow-up: #2445 ### Testing - Integration test, for static and browser requests, that the sub crawlers send through the HTTP client the crawler closes. *✍️ Drafted by Claude Code*
1 parent e24df49 commit fddc577

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎src/crawlee/crawlers/_adaptive_playwright/_adaptive_playwright_crawler.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ def __init__(
179179
# Sub crawlers related.
180180
playwright_crawler_specific_kwargs = playwright_crawler_specific_kwargs or _PlaywrightCrawlerAdditionalOptions()
181181

182+
# The sub crawlers never run, so they would never enter or close defaults of their own. The client of the
183+
# adaptive crawler already serves `send_request` in their contexts.
184+
kwargs['session_pool'] = self._session_pool
185+
kwargs['http_client'] = self._http_client
186+
182187
# The sub crawlers log under the adaptive crawler's logger, and only errors, since the adaptive crawler reports
183188
# the outcome of each request itself.
184189
static_logger = self._logger.getChild('static')

‎tests/integration/crawlers/_adaptive_playwright/test_adaptive_playwright_crawler.py‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from crawlee.crawlers._adaptive_playwright._adaptive_playwright_crawling_context import AdaptiveContextError
3131
from crawlee.crawlers._adaptive_playwright._rendering_type_predictor import DefaultRenderingTypePredictor
3232
from crawlee.errors import RequestThrottledError
33-
from crawlee.http_clients import HttpClient, HttpResponse
33+
from crawlee.http_clients import HttpClient, HttpResponse, ImpitHttpClient
3434
from crawlee.sessions import SessionPool
3535
from crawlee.statistics import Statistics
3636
from crawlee.storage_backends import SqlStorageBackend
@@ -720,6 +720,33 @@ async def request_handler(context: AdaptivePlaywrightCrawlingContext) -> None:
720720
mocked_browser_handler.assert_called_once_with()
721721

722722

723+
async def test_static_sub_crawler_sends_through_crawler_http_client(test_urls: list[str]) -> None:
724+
"""The static sub crawler sends through the default HTTP client of the crawler, which closes only that client."""
725+
crawler = AdaptivePlaywrightCrawler.with_beautifulsoup_static_parser(
726+
rendering_type_predictor=_SimpleRenderingTypePredictor(
727+
rendering_types=cycle(['static']), detection_probability_recommendation=cycle([0])
728+
),
729+
)
730+
731+
@crawler.router.default_handler
732+
async def request_handler(context: AdaptivePlaywrightCrawlingContext) -> None:
733+
pass
734+
735+
with (
736+
patch.object(
737+
ImpitHttpClient, 'send_request', autospec=True, side_effect=ImpitHttpClient.send_request
738+
) as send_request,
739+
patch.object(ImpitHttpClient, 'cleanup', autospec=True, side_effect=ImpitHttpClient.cleanup) as cleanup,
740+
):
741+
stats = await crawler.run(test_urls[:1])
742+
743+
sending_clients = {send_call.args[0] for send_call in send_request.await_args_list}
744+
closed_clients = {cleanup_call.args[0] for cleanup_call in cleanup.await_args_list}
745+
assert stats.requests_finished == 1
746+
assert sending_clients == {crawler._http_client}
747+
assert closed_clients == {crawler._http_client}
748+
749+
723750
async def test_adaptive_context_query_selector_beautiful_soup(test_urls: list[str]) -> None:
724751
"""Test that `context.query_selector_one` works regardless of the crawl type for BeautifulSoup variant.
725752

0 commit comments

Comments
 (0)