Skip to content

Commit

Permalink
Override method and/or body only for the first matching request
Browse files Browse the repository at this point in the history
  • Loading branch information
elacuesta committed Jul 11, 2024
1 parent f3b1b25 commit 6d23d0e
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions scrapy_playwright/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,13 @@ async def _download_request(self, request: Request, spider: Spider) -> Response:
page=page, request=request, spider=spider, context_name=context_name
)

# We need to identify the Playwright request that matches the Scrapy request
# in order to override method and body if necessary.
# Checking the URL and Request.is_navigation_request() is not enough, e.g.
# requests produced by submitting forms can produce false positives.
# Let's track only the first request that matches the above conditions.
initial_request_done = asyncio.Event()

await page.unroute("**")
await page.route(
"**",
Expand All @@ -368,6 +375,7 @@ async def _download_request(self, request: Request, spider: Spider) -> Response:
body=request.body,
encoding=request.encoding,
spider=spider,
initial_request_done=initial_request_done,
),
)

Expand Down Expand Up @@ -637,6 +645,7 @@ def _make_request_handler(
body: Optional[bytes],
encoding: str,
spider: Spider,
initial_request_done: asyncio.Event,
) -> Callable:
async def _request_handler(route: Route, playwright_request: PlaywrightRequest) -> None:
"""Override request headers, method and body."""
Expand Down Expand Up @@ -676,7 +685,9 @@ async def _request_handler(route: Route, playwright_request: PlaywrightRequest)
if (
playwright_request.url.rstrip("/") == url.rstrip("/")
and playwright_request.is_navigation_request()
and not initial_request_done.is_set()
):
initial_request_done.set()
if method.upper() != playwright_request.method.upper():
overrides["method"] = method
if body:
Expand Down

0 comments on commit 6d23d0e

Please sign in to comment.