> For the complete documentation index, see [llms.txt](https://developers.oxylabs.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.oxylabs.io/products/cn/headless-browser/optimizing-traffic.md).

# 优化流量

你的自动化脚本经常会下载不必要的资源，例如图片、样式表、字体和其他媒体文件。这些文件会消耗带宽，并可能拖慢你的爬取操作。

你可以完全阻止这些不必要的资源加载。通过拦截网络请求并有选择地屏蔽媒体文件，你可以只关注真正需要的数据。

这些代码片段同时适用于 Playwright 和 Puppeteer，可在媒体资源下载前将其阻止：

{% tabs %}
{% tab title="Python" %}

```python
async def block_resources(route):
    request = route.request
    resource_type = request.resource_type
    if resource_type in ['image', 'stylesheet', 'media', 'font']:
        await route.abort()
    else:
        await route.continue_()
await page.route('**/*', block_resources)
```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
await page.route('**/*', (route) => {
    const request = route.request();
    const type = request.resourceType();

    if (['image', 'stylesheet', 'media', 'font'].includes(type)) {
        return route.abort();
    }

    return route.continue();
});
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developers.oxylabs.io/products/cn/headless-browser/optimizing-traffic.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
