跳转至

Fetch 命令

Fetch 命令使用 Fetch API 域提供高级网络请求处理和拦截功能。

概述

Fetch 命令模块支持复杂的网络请求管理,包括请求修改、响应拦截和身份验证处理。

pydoll.commands.fetch_commands

FetchCommands

This class encapsulates the fetch commands of the Chrome DevTools Protocol (CDP).

CDP's Fetch domain allows interception and modification of network requests at the application layer. This enables developers to examine, modify, and control network traffic, which is particularly useful for testing, debugging, and advanced automation scenarios.

The commands defined in this class provide functionality for: - Enabling and disabling fetch request interception - Continuing, fulfilling, or failing intercepted requests - Handling authentication challenges - Retrieving and modifying response bodies - Processing response data as streams

continue_request staticmethod

continue_request(request_id, url=None, method=None, post_data=None, headers=None, intercept_response=None)

Creates a command to continue a paused fetch request.

This command allows the browser to resume a fetch operation that has been intercepted. You can modify the fetch request URL, method, headers, and body before continuing.

PARAMETER DESCRIPTION
request_id

The ID of the fetch request to continue.

TYPE: str

url

The new URL for the fetch request. Defaults to None.

TYPE: Optional[str] DEFAULT: None

method

The HTTP method to use (e.g., 'GET', 'POST'). Defaults to None.

TYPE: Optional[RequestMethod] DEFAULT: None

post_data

The body data to send with the fetch request. Defaults to None.

TYPE: Optional[dict] DEFAULT: None

headers

A list of HTTP headers to include in the fetch request. Defaults to None.

TYPE: Optional[list[HeaderEntry]] DEFAULT: None

intercept_response

Indicates if the response should be intercepted. Defaults to None.

TYPE: Optional[bool] DEFAULT: None

RETURNS DESCRIPTION
Command[Response]

Command[Response]: A command for continuing the fetch request.

continue_request_with_auth staticmethod

continue_request_with_auth(request_id, auth_challenge_response, proxy_username=None, proxy_password=None)

Creates a command to continue a paused fetch request with authentication.

This command is used when the fetch operation requires authentication. It provides the necessary credentials to continue the request.

PARAMETER DESCRIPTION
request_id

The ID of the fetch request to continue.

TYPE: str

auth_challenge_response

The authentication challenge response type.

TYPE: AuthChallengeResponseValues

proxy_username

The username for proxy authentication. Defaults to None.

TYPE: Optional[str] DEFAULT: None

proxy_password

The password for proxy authentication. Defaults to None.

TYPE: Optional[str] DEFAULT: None

RETURNS DESCRIPTION
Command[Response]

Command[Response]: A command for continuing the fetch request with authentication.

disable staticmethod

disable()

Creates a command to disable fetch interception.

This command stops the browser from intercepting fetch requests.

RETURNS DESCRIPTION
Command[Response]

Command[Response]: A command for disabling fetch interception.

enable staticmethod

enable(handle_auth_requests, url_pattern='*', resource_type=None, request_stage=None)

Creates a command to enable fetch interception.

This command allows the browser to start intercepting fetch requests. You can specify whether to handle authentication challenges and the types of resources to intercept.

PARAMETER DESCRIPTION
handle_auth_requests

Indicates if authentication requests should be handled.

TYPE: bool

url_pattern

Pattern to match URLs for interception. Defaults to '*'.

TYPE: str DEFAULT: '*'

resource_type

The type of resource to intercept. Defaults to None.

TYPE: Optional[ResourceType] DEFAULT: None

request_stage

The stage of the request to intercept. Defaults to None.

TYPE: Optional[RequestStage] DEFAULT: None

RETURNS DESCRIPTION
Command[Response]

Command[Response]: A command for enabling fetch interception.

fail_request staticmethod

fail_request(request_id, error_reason)

Creates a command to simulate a failure in a fetch request.

This command allows you to simulate a failure for a specific fetch operation, providing a reason for the failure.

PARAMETER DESCRIPTION
request_id

The ID of the fetch request to fail.

TYPE: str

error_reason

The reason for the failure.

TYPE: NetworkErrorReason

RETURNS DESCRIPTION
Command[Response]

Command[Response]: A command for failing the fetch request.

fulfill_request staticmethod

fulfill_request(request_id, response_code, response_headers=None, body=None, response_phrase=None)

Creates a command to fulfill a fetch request with a custom response.

This command allows you to provide a custom response for a fetch operation, including the HTTP status code, headers, and body content.

PARAMETER DESCRIPTION
request_id

The ID of the fetch request to fulfill.

TYPE: str

response_code

The HTTP status code to return.

TYPE: int

response_headers

A list of response headers. Defaults to None.

TYPE: Optional[list[HeaderEntry]] DEFAULT: None

body

The body content of the response. Defaults to None.

TYPE: Optional[dict] DEFAULT: None

response_phrase

The response phrase (e.g., 'OK', 'Not Found'). Defaults to None.

TYPE: Optional[str] DEFAULT: None

RETURNS DESCRIPTION
Command[Response]

Command[Response]: A command for fulfilling the fetch request.

get_response_body staticmethod

get_response_body(request_id)

Creates a command to retrieve the response body of a fetch request.

This command allows you to access the body of a completed fetch operation, which can be useful for analyzing the response data.

PARAMETER DESCRIPTION
request_id

The ID of the fetch request to retrieve the body from.

TYPE: str

RETURNS DESCRIPTION
Command[GetResponseBodyResponse]

Command[GetResponseBodyResponse]: A command for getting the response body.

continue_response staticmethod

continue_response(request_id, response_code=None, response_headers=None, response_phrase=None)

Creates a command to continue a fetch response for an intercepted request.

This command allows the browser to continue the response flow for a specific fetch request, including customizing the HTTP status code, headers, and response phrase.

PARAMETER DESCRIPTION
request_id

The ID of the fetch request to continue the response for.

TYPE: str

response_code

The HTTP status code to send. Defaults to None.

TYPE: Optional[int] DEFAULT: None

response_headers

A list of response headers. Defaults to None.

TYPE: Optional[list[HeaderEntry]] DEFAULT: None

response_phrase

The response phrase (e.g., 'OK'). Defaults to None.

TYPE: Optional[str] DEFAULT: None

RETURNS DESCRIPTION
Command[Response]

Command[Response]: A command for continuing the fetch response.

take_response_body_as_stream staticmethod

take_response_body_as_stream(request_id)

Creates a command to take the response body as a stream.

This command allows you to receive the response body as a stream which can be useful for handling large responses.

PARAMETER DESCRIPTION
request_id

The ID of the fetch request to take the response body stream from.

TYPE: str

RETURNS DESCRIPTION
Command[TakeResponseBodyAsStreamResponse]

Command[TakeResponseBodyAsStreamResponse]: A command for taking the response body as a stream.

用法

Fetch 命令用于高级网络拦截和请求处理:

from pydoll.commands.fetch_commands import enable, request_paused, continue_request
from pydoll.connection.connection_handler import ConnectionHandler

# Enable fetch domain
connection = ConnectionHandler()
await enable(connection, patterns=[{
    "urlPattern": "*",
    "requestStage": "Request"
}])

# Handle paused requests
async def handle_paused_request(request_id, request):
    # Modify request or continue as-is
    await continue_request(connection, request_id=request_id)

关键功能

fetch 命令模块提供以下功能:

请求拦截

  • enable() - 激活fetch模式
  • disable() - 关闭fetch模式
  • continue_request() - 继续请求(放行)
  • fail_request() - 返回特定错误请求

修改请求

  • 修改请求headers
  • 更改请求 URL
  • 更改请求方法(GET、POST 等)
  • 修改请求body

响应处理

  • fulfill_request() - 提供自定义响应
  • get_response_body() - 获取响应内容
  • 修改响应头
  • 响应状态码控制

身份验证

  • continue_with_auth() - 处理身份验证挑战
  • 基本身份验证支持
  • 自定义身份验证流程

高级功能

基于模式的拦截

# Intercept specific URL patterns
patterns = [
    {"urlPattern": "*/api/*", "requestStage": "Request"},
    {"urlPattern": "*.js", "requestStage": "Response"},
    {"urlPattern": "https://example.com/*", "requestStage": "Request"}
]

await enable(connection, patterns=patterns)

请求修改

# Modify intercepted requests
async def modify_request(request_id, request):
    # Add authentication header
    headers = request.headers.copy()
    headers["Authorization"] = "Bearer token123"

    # Continue with modified headers
    await continue_request(
        connection,
        request_id=request_id,
        headers=headers
    )

响应模拟

# Mock API responses
await fulfill_request(
    connection,
    request_id=request_id,
    response_code=200,
    response_headers=[
        {"name": "Content-Type", "value": "application/json"},
        {"name": "Access-Control-Allow-Origin", "value": "*"}
    ],
    body='{"status": "success", "data": {"mocked": true}}'
)

身份验证处理

# Handle authentication challenges
await continue_with_auth(
    connection,
    request_id=request_id,
    auth_challenge_response={
        "response": "ProvideCredentials",
        "username": "user",
        "password": "pass"
    }
)

请求阶段

Fetch 命令可以在不同阶段拦截请求:

阶段 描述 用例
请求 请求发送前 修改标头、URL 和方法
响应 收到响应后 模拟响应,修改内容

错误处理

# Fail requests with specific errors
await fail_request(
    connection,
    request_id=request_id,
    error_reason="ConnectionRefused"  # or "AccessDenied", "TimedOut", etc.
)

与网络命令集成

Fetch 命令与网络命令协同工作,但提供更精细的控制:

  • 网络命令:更广泛的网络监控和控制
  • Fetch 命令:特定的请求/响应拦截和修改

Performance Considerations

Fetch interception can impact page loading performance. Use specific URL patterns and disable when not needed to minimize overhead.