# OxyCopilot

**OxyCopilot** 是一个免费的 [**网页爬虫API** ](/products/cn/web-scraper-api.md)功能，可简化入门流程，并帮助用户为复杂用例找到有效解决方案，而且无需具备编码知识。OxyCopilot 目前包含三个独立功能：

* **爬虫构建器**
* [**自定义解析器**](/products/cn/web-scraper-api/features/custom-parser.md) **构建器**
* **浏览器指令构建器**

{% hint style="success" %}
OxyCopilot 可在以下位置访问： [**网页** **网页爬虫API Playground**](https://dashboard.oxylabs.io/?route=/api-playground) 在 Oxylabs 控制台中。
{% endhint %}

{% embed url="<https://youtu.be/9JoF8_5r5HY?si=61c3Zkx6FrH06PVa>" %}

## 爬虫构建器

OxyCopilot 可帮助你为网页爬虫API配置爬虫（并构建请求负载），无需理解文档或字段逻辑。

### 工作原理

#### **步骤 1：提供 URL 和提示词**

* **URL：** 提供你想要抓取的 URL。
* **提示词：** 描述你的需求（例如，本地化、JS 渲染等）。

<figure><img src="/files/d9a7b70a5b19ae937aef5bf5345f1a43ac662417" alt="" width="563"><figcaption></figcaption></figure>

**步骤 2：解析**

你有三种处理解析的选项：

1. **自定义解析器**：选择“添加解析指令”以使用 [**自定义解析器构建器**](#custom-parser-builder).
2. **专用解析器**：如果该 URL 来自我们提供专用解析器的网站，并且你想使用它，请选择“继续使用专用解析器”。
3. **不解析**：如果不需要结构化数据，可选择在不解析的情况下继续。

{% hint style="warning" %}
如果该 URL 属于我们拥有专用解析器的网站，但你不需要结构化数据，请选择“继续使用专用解析器”，并在 playground 设置中禁用 parse 参数。避免使用退出按钮，因为它不会保存预填参数。
{% endhint %}

<div><figure><img src="/files/19a29608a29ddeb8b9dc624817fa98be8edfe2d6" alt="" width="375"><figcaption><p>如果我们没有专用解析器</p></figcaption></figure> <figure><img src="/files/9a8ebd5dc4af699d5eb992779684648a62d7e309" alt="" width="375"><figcaption><p>如果我们有专用解析器</p></figcaption></figure></div>

#### **步骤 3：检查请求**

根据你的提示词，OxyCopilot 将在网页爬虫API Playground 中预填所需参数。你将看到适用于你的用例的具体请求代码和参数，并且可以在需要时调整参数。

**步骤 4：提交请求并复制**

如果一切看起来都没问题，提交请求以查看输出效果，并检查其是否按预期工作。然后，复制请求代码，以便在后续使用网页爬虫API执行抓取任务时使用。

### 示例

#### URL

```
https://www.amazon.de/s?k=adidas
```

#### 提示词

{% code overflow="wrap" %}

```
抓取所提供 URL 中的 Amazon 搜索页面，并将结果本地化为波兰。
```

{% endcode %}

#### AI 生成的参数（JSON）

```json
{
        "source": "amazon_search",
        "query": "adidas",
        "geo_location": "PL",
        "domain": "de"
}
```

#### AI 生成的请求代码

<figure><img src="/files/89992d23d3dc866fbc43a984bd64dcdadce02561" alt=""><figcaption></figcaption></figure>

## 自定义解析器构建器

利用 [**自定义解析器**](/products/cn/web-scraper-api/features/custom-parser.md) 功能，配合 OxyCopilot 构建解析器，而无需编写代码或手动分析网站结构。

### 工作原理

#### **步骤 1：提供 URL 和提示词**

* **URL：** 你最多可以提供 **3 个 URL** ，为其生成解析指令。OxyCopilot 会使用所提供 URL 的 HTML 来确定提取所需字段的最佳逻辑。&#x20;

{% hint style="info" %}
你提供的 URL 越多，解析指令就会越稳健，因为 OxyCopilot 会识别相似页面中的共通模式。请注意，额外的 URL 可能会增加结果等待时间。
{% endhint %}

* **提示词：** 提示词是构建自然语言 schema 的关键组成部分，它是生成实际解析指令的基础。提示词应清楚描述需要解析的字段。

<figure><img src="/files/d1ab8653f53b017dfb2b62d0bd95a2f7c4508dfa" alt=""><figcaption></figcaption></figure>

#### **步骤 2【可选】：调整解析 schema**

此步骤允许你微调解析 schema，以更好地满足你的需求或排查问题。

#### **解析 schema 概览**

<figure><img src="/files/128c44d0a31cb949cfcf741429487ebda1442f5e" alt=""><figcaption></figcaption></figure>

此表展示了 AI 用于生成解析指令的输入内容。schema 定义了需要解析哪些字段，并由多种对象类型组成（见下方 [**表格**](#object-type-explanations) 中的说明）。

schema 中的每个项目都必须具有：

* **名称**：这将用作解析指令中的对象键，并显示在解析后的数据中。
* **描述** （可选但推荐）：有助于提高解析准确性。

### **Schema 调整**

* **重新排序项目**：使用左侧的圆点拖放项目以更改其顺序（只能移动同一嵌套层级内的项目）。
* **编辑项目**：点击编辑图标以修改任意字段。
* **删除项目**：你可以删除父级中的任意项目。
* **添加新项目**：向父级添加新项目。

更新 schema 后，点击 **“刷新输出”** 按钮以重新生成指令并预览解析后的数据。

### 对象类型说明

<table><thead><tr><th width="208">对象类型</th><th width="243">描述</th><th>解析后的数据示例</th></tr></thead><tbody><tr><td>字符串</td><td>单个文本输出</td><td><code>“title”: “示例产品标题”</code></td></tr><tr><td>数字</td><td>单个数字</td><td><code>“price”: 9.99</code></td></tr><tr><td>字符串数组</td><td>文本输出列表</td><td><code>“products”: [“产品 1”, “产品 2”, “产品 3”]</code></td></tr><tr><td>数字数组</td><td>数字列表</td><td><code>“pages”: [1, 2, 3]</code></td></tr><tr><td>对象数组</td><td>对象/项目列表，其中每个项目内部都有自己的对象（<code>_items</code> 解析指令中的 block）</td><td><p></p><pre class="language-json"><code class="lang-json">“related_items”: [
  {
    “title”: “产品 1”,
    “price”: 9.99
  },
  {
    “title”: “产品 2”,
    “price”: 15.99
  }
]
</code></pre></td></tr></tbody></table>

### 处理对象数组

1. **选择“对象数组”**：此选项会添加一个子对象和按钮。

<figure><img src="https://lh7-qw.googleusercontent.com/docsz/AD_4nXcnZ-xxFBAjZPzSJesa5bjbUj7wOQlGn7Ut4bxQzrRNbUmN0CkcfOZa23QRLma2vUsINNl6c5TOixopuBGdIk9iKFvWNpfpkF5s-zL9CKWxEEeJ40yZc6n2eqRsUw45HcWJjZikl4pERT-8-nF5Pno7kpQ?key=TW5rMlJ-s_BzFm7nRv1Dlw" alt=""><figcaption></figcaption></figure>

2. **填写对象名称**：要将项目保存到 schema 中，你必须填写父对象和子对象的名称。完成后，复选标记将变为绿色。

<figure><img src="https://lh7-qw.googleusercontent.com/docsz/AD_4nXffrjzhyFW4oiVj6MHaRGp7ysfkC1cVR4viQEWM5FBE3vhElH-ZRL5B796G6cfK5dNMvLtXafioTUoQaG-3QQTuaPLcq4UcsmA524hNW_IMjvw6pUdY-CRAHaYvyMkctNX0pp9qmWrxoOR3sNAwqwc8OpVT?key=TW5rMlJ-s_BzFm7nRv1Dlw" alt=""><figcaption></figcaption></figure>

3. **子对象要求**：“对象数组”必须至少有一个子项。

### 测试指令

默认情况下，解析后的数据基于 **步骤 1**中提供的第一个 URL。你也可以提供其他 URL 来测试解析指令：

<figure><img src="/files/9c3a55c810a6c162bb712202b78ce8299f403f95" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
指令是基于初始 URL 生成的，不会考虑测试 URL。编辑提示词或 URL 将重置 schema，并需要重新完整生成。
{% endhint %}

#### **步骤 3：复制/保存指令并集成到抓取任务中**

当指令令人满意后：

* 使用 **“复制”** 按钮以复制指令，并将其粘贴到你的爬虫代码中。
* 或者，将指令保存到你的网页爬虫API Playground 会话中，调整其他请求参数，进行测试，然后复制你偏好编程语言的完整请求代码。

<figure><img src="/files/d04f3a131e851010bfab905a8d79fb49564f340a" alt=""><figcaption></figcaption></figure>

### 示例

#### URL

```
https://sandbox.oxylabs.io/products/1
```

#### 提示词

{% code overflow="wrap" %}

```
我想解析一个产品页面。解析后的数据应包含以下字段：

- product_title：包含产品标题的文本字段
- price：包含产品价格的数字字段
- related_products：一个列表，包含显示在主要产品信息下方的相关产品标题
```

{% endcode %}

#### 解析 schema

<table><thead><tr><th width="214">对象类型</th><th width="209">名称*</th><th>描述</th></tr></thead><tbody><tr><td>字符串</td><td>product_title</td><td>产品标题</td></tr><tr><td>数字</td><td>price</td><td>产品价格</td></tr><tr><td>字符串数组</td><td>related_products</td><td>主要产品信息下方的相关产品标题</td></tr></tbody></table>

#### 解析指令

```json
{
    "product_title": {
        "_fns": [
            {
                "_fn": "xpath_one",
                "_args": [
                    "//h2[@class=\"title css-1k75zwy e1pl6npa11\"]/text()",
                    "//div[@class=\"product-info-wrapper css-m2w3q2 emlf3670\"]/h2/text()",
                    "//div[@id=\"__next\"]/main/div/div/div/div[2]/div[1]/div[2]/div[2]/h2/text()"
                ]
            },
            {
                "_fn": "regex_search",
                "_args": [
                    "^\\s*(.[\\s\\S]*?)\\s*$",
                    1
                ]
            }
        ]
    },
    "price": {
        "_fns": [
            {
                "_fn": "xpath_one",
                "_args": [
                    "//div[@class=\"price css-o7uf8d e1pl6npa6\"]/text()",
                    "//div[@class=\"product-info-wrapper css-m2w3q2 emlf3670\"]/div[4]/text()",
                    "//div[@id=\"__next\"]/main/div/div/div/div[2]/div[1]/div[2]/div[2]/div[4]/text()"
                ]
            },
            {
                "_fn": "amount_from_string"
            }
        ]
    },
    "related_products": {
        "_fns": [
            {
                "_fn": "xpath",
                "_args": [
                    "//div/div[@class=\"product-card css-e8at8d eag3qlw10\"]/a[1]/h4/text()",
                    "//div[@id=\"__next\"]/main/div/div/div/div[2]/div[2]/div/a[1]/h4/text()",
                    "//div[@class=\"related-products css-1rinft1 emlf3670\"]/div/a[1]/h4/text()",
                    "//html[@lang=\"en\"]/body/div/main/div/div/div/div[2]/div[2]/div/a[1]/h4/text()",
                    "//div/div[@class=\"product-card css-e8at8d eag3qlw10\"]//h4[@class=\"title css-7u5e79 eag3qlw7\"]/text()",
                    "//div[@id=\"__next\"]/main/div/div/div/div[2]/div[2]/div//h4[@class=\"title css-7u5e79 eag3qlw7\"]/text()",
                    "//div[@class=\"related-products css-1rinft1 emlf3670\"]/div//h4[@class=\"title css-7u5e79 eag3qlw7\"]/text()",
                    "//div/div[@class=\"product-card css-e8at8d eag3qlw10\"]//a[@class=\"card-header css-o171kl eag3qlw2\"]/h4/text()",
                    "//html[@lang=\"en\"]/body/div/main/div/div/div/div[2]/div[2]/div//h4[@class=\"title css-7u5e79 eag3qlw7\"]/text()",
                    "//div[@id=\"__next\"]/main/div/div/div/div[2]/div[2]/div//a[@class=\"card-header css-o171kl eag3qlw2\"]/h4/text()",
                    "//div[@class=\"related-products css-1rinft1 emlf3670\"]/div//a[@class=\"card-header css-o171kl eag3qlw2\"]/h4/text()",
                    "//html[@lang=\"en\"]/body/div/main/div/div/div/div[2]/div[2]/div//a[@class=\"card-header css-o171kl eag3qlw2\"]/h4/text()"
                ]
            },
            {
                "_fn": "regex_search",
                "_args": [
                    "^\\s*(.[\\s\\S]*?)\\s*$",
                    1
                ]
            }
        ]
    }
}
```

#### 解析后的数据

```json
{
    "price": 91.99,
    "product_title": "塞尔达传说：时之笛",
    "related_products": [
        "塞尔达传说：姆吉拉的假面",
        "印第安纳·琼斯与地狱机器"
    ],
    "解析状态码": 12000
}
```

### 通过 API 生成解析指令

如果你想生成大量不同的解析指令集，以覆盖你处理的各类网站，你可以通过 API 构建解析指令——请参阅 [解析指令生成器 API](/products/cn/web-scraper-api/features/custom-parser/generating-parsing-instructions-via-api.md) 了解其实现方式。

## 浏览器指令构建器

你可以使用 OxyCopilot 构建复杂的页面交互脚本，而无需分析网站结构或手动为你的 [浏览器指令](/products/cn/web-scraper-api/features/js-rendering-and-browser-control.md).

### 工作原理

#### **步骤 1：提供 URL 和提示词**

* **URL：** 请提供一个单独的 URL 以生成浏览器指令。OxyCopilot 会使用所提供 URL 的 HTML 来确定如何编写你所需的网页交互脚本。&#x20;
* **提示词：** 提示词对于构建浏览器指令至关重要。请清楚说明你希望在网页打开后执行哪些操作（例如“滚动到底部，等待‘下一页’按钮加载，然后点击‘下一页’按钮”）。

<figure><img src="/files/c670fbfbc7340ba56e087ef26cc33b531eed80ff" alt=""><figcaption></figcaption></figure>

#### **步骤 2【可选】：调整浏览器指令**

此步骤允许你微调浏览器指令序列，以更好地满足你的需求或排查问题。

#### **浏览器指令概览**

<figure><img src="/files/f79a79e0ac62f9498f382e3e477a14d459007dd4" alt=""><figcaption></figcaption></figure>

当 OxyCopilot 处理完你的输入后，它会显示其创建的浏览器指令序列。

你可以通过编辑、添加或删除步骤来调整该序列。

#### **步骤 3：复制/保存指令并集成到抓取任务中**

当指令令人满意后，你可以将其保存到你的网页爬虫API Playground 会话中，调整其他请求参数，进行测试，然后复制你偏好编程语言的完整请求代码。

<figure><img src="/files/8b66449d96222b444ec6866b9bcb81653b3a8850" alt=""><figcaption></figcaption></figure>

{% hint style="success" %}
我们欢迎你的反馈和改进建议。如有需要，请随时通过以下方式联系我们： <support@oxylabs.io> 或联系我们的 24/7 在线聊天支持。
{% endhint %}


---

# 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/web-scraper-api/web-scraper-api-playground/oxycopilot.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.
