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

# OxyCopilot

使用 OxyCopilot 自动化爬取。学习如何通过简单的自然语言提示生成网页爬虫API 载荷、自定义解析器和浏览器指令。

**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="https://3714446197-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBQ7Zf9paoN3FTeGcyfY1%2Fuploads%2Fgit-blob-0fa03898183f16364144407a501c8862c4db00da%2FScreenshot%202024-09-24%20at%2016.19.08.png?alt=media" 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="https://3714446197-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBQ7Zf9paoN3FTeGcyfY1%2Fuploads%2Fgit-blob-19dcd4cb01214326b5012923a5d4047f37d88259%2FScreenshot%202024-09-24%20at%2016.20.09.png?alt=media" alt="" width="375"><figcaption><p>如果我们没有专用解析器</p></figcaption></figure> <figure><img src="https://3714446197-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBQ7Zf9paoN3FTeGcyfY1%2Fuploads%2Fgit-blob-be0b87f84120bc3f31d7b5e30ab75fcd2bd108d2%2FScreenshot%202024-09-24%20at%2016.19.36.png?alt=media" 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="https://3714446197-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBQ7Zf9paoN3FTeGcyfY1%2Fuploads%2Fgit-blob-ae742ee398acbc33744d7043c174f6638e861b8d%2FScreenshot%202024-09-24%20at%2016.44.00.png?alt=media" alt=""><figcaption></figcaption></figure>

## 自定义解析器构建器

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

### 工作原理

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

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

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

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

<figure><img src="https://3714446197-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBQ7Zf9paoN3FTeGcyfY1%2Fuploads%2Fgit-blob-7333b7f9585928fe89f7fb909de3c0d372f61a38%2FScreenshot%202024-09-24%20at%2017.57.15.png?alt=media" alt=""><figcaption></figcaption></figure>

#### **步骤 2 \[可选]：调整解析 schema**

这一步可让你微调解析 schema，以更好满足需求或排查问题。

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

<figure><img src="https://3714446197-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBQ7Zf9paoN3FTeGcyfY1%2Fuploads%2Fgit-blob-8b2b173288abf3f88ec7ea2d7983d0e1f94e100b%2FScreenshot%202024-09-24%20at%2017.59.27.png?alt=media" 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> 解析指令中的区块）</td><td><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="https://3714446197-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBQ7Zf9paoN3FTeGcyfY1%2Fuploads%2Fgit-blob-58535851049d3c5f9671ea984a07db9bcbcb1862%2FScreenshot%202024-09-24%20at%2018.14.45.png?alt=media" alt=""><figcaption></figcaption></figure>

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

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

指令满意后：

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

<figure><img src="https://3714446197-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBQ7Zf9paoN3FTeGcyfY1%2Fuploads%2Fgit-blob-61b517e5299a637f2f38c497e8f9c0bb94819268%2FScreenshot%202024-09-24%20at%2018.17.04.png?alt=media" 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": "The Legend of Zelda: Ocarina of Time",
    "related_products": [
        "The Legend of Zelda: Majora's Mask",
        "Indiana Jones and the Infernal Machine"
    ],
    "parse_status_code": 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 来确定如何编排你所需的网页交互脚本。
* **提示：** 提示对于构建浏览器指令至关重要。请清楚说明页面打开后你希望执行哪些操作（例如“滚动到页面底部，等待“下一页”按钮加载，然后点击“下一页”按钮”）。

<figure><img src="https://3714446197-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBQ7Zf9paoN3FTeGcyfY1%2Fuploads%2Fgit-blob-8dba0a7e0afde23d1884daae65c645fb5c5d64ea%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

#### **步骤 2 \[可选]：调整浏览器指令**

这一步可让你微调浏览器指令序列，以更好满足需求或排查问题。

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

<figure><img src="https://3714446197-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBQ7Zf9paoN3FTeGcyfY1%2Fuploads%2Fgit-blob-e3e70cf09f00b2904e53a0c2adc3370e7f702a51%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

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

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

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

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

<figure><img src="https://3714446197-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBQ7Zf9paoN3FTeGcyfY1%2Fuploads%2Fgit-blob-d26523d746d989319454010151a75d415586c54e%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

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


---

# 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, and the optional `goal` query parameter:

```
GET https://developers.oxylabs.io/products/cn/web-scraper-api/web-scraper-api-playground/oxycopilot.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
