# 流量优化

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

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

这些代码片段可与 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: 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.
