diff --git a/src/crawlee/crawlers/_playwright/_types.py b/src/crawlee/crawlers/_playwright/_types.py index 01721bf373..393e6f5a8d 100644 --- a/src/crawlee/crawlers/_playwright/_types.py +++ b/src/crawlee/crawlers/_playwright/_types.py @@ -3,13 +3,15 @@ from dataclasses import dataclass from typing import TYPE_CHECKING, Literal, Protocol, TypedDict +from playwright.async_api import APIResponse + from crawlee import HttpHeaders from crawlee._utils.docs import docs_group if TYPE_CHECKING: from collections.abc import AsyncGenerator - from playwright.async_api import APIResponse, Response + from playwright.async_api import Response from typing_extensions import NotRequired, Self @@ -56,6 +58,9 @@ async def from_playwright_response(cls, response: Response | APIResponse, protoc # Used http protocol version cannot be obtained from `Response` and has to be passed as additional argument. http_version = protocol _content = await response.body() + # If not called then the body will stay in memory until the context closes. + if isinstance(response, APIResponse): + await response.dispose() return cls(http_version=http_version, status_code=status_code, headers=headers, _content=_content)