# 通过 API 生成解析指令

您可以通过 API 生成解析指令集：提供 URL，并描述您希望解析哪些数据点。收到生成的解析指令后，您可以将其保存为一个 [解析器预设](/products/cn/web-scraper-api/features/custom-parser/parser-presets.md) ，或者直接将这些指令与您的抓取请求一并发送。

您也可以通过 [OxyCopilot](/products/cn/web-scraper-api/web-scraper-api-playground/oxycopilot.md) 在我们的网页爬虫API Playground 中生成解析指令。

## 从提示生成指令

您可以通过输入对希望解析的数据点的自由文本描述，并提供几个属于同一页面类型的 URL 来生成解析指令。API 将返回一组解析指令。

* **端点**: `https://data.oxylabs.io/v1/parsers/generate-instructions/prompt`
* **方法**: `POST`
* **身份验证**: `基础`
* **请求头**: `Content-Type: application/json`

### 示例负载

```json
{ 
  "prompt_text": "解析产品标题、主价格、开发者名称和平台名称。",
  "urls": [
    "https://sandbox.oxylabs.io/products/1",
    "https://sandbox.oxylabs.io/products/2",
    "https://sandbox.oxylabs.io/products/4"
  ],
  "render": false
}
```

<table><thead><tr><th width="177.1328125">参数</th><th>描述</th></tr></thead><tbody><tr><td><mark style="color:默认;background-color:green;"><strong><code>prompt_text</code></strong></mark></td><td>要解析的数据点的自由文本描述。</td></tr><tr><td><mark style="color:默认;background-color:green;"><strong><code>urls</code></strong></mark></td><td>用于示例说明您希望获取解析指令的页面类型的 URL 列表。我们建议提供 3-5 个 URL，以帮助解析器适应不同布局并提高解析准确性。</td></tr><tr><td><code>render</code></td><td>是否应使用 JS 渲染来获取所需内容。 </td></tr></tbody></table>

&#x20;    \- 必填参数

### 示例响应

```json
{
    "parsing_instructions": {
        "developer_name": {
            "_fns": [
                {
                    "_args": [
                        "//div[contains(@class, 'brand-wrapper')]//span[@class='brand developer']"
                    ],
                    "_fn": "xpath"
                },
                {
                    "_args": [
                        "normalize-space(.)"
                    ],
                    "_fn": "xpath"
                },
                {
                    "_args": " ",
                    "_fn": "join"
                }
            ]
        },
        "main_price": {
            "_fns": [
                {
                    "_args": [
                        "//div[contains(@class, 'product-info-wrapper')]//div[contains(@class, 'price')]/text()"
                    ],
                    "_fn": "xpath_one"
                },
                {
                    "_fn": "amount_from_string"
                }
            ]
        },
        "title": {
            "_fns": [
                {
                    "_args": [
                        "//div[contains(@class, 'product-info-wrapper')]//h2/text()"
                    ],
                    "_fn": "xpath_one"
                },
                {
                    "_args": [
                        "^\\s*(.[\\s\\S]*?)\\s*$",
                        1
                    ],
                    "_fn": "regex_search"
                }
            ]
        }
    },
    "prompt_schema": {
        "properties": {
            "developer_name": {
                "description": "开发者名称。",
                "title": "开发者名称",
                "type": "string"
            },
            "main_price": {
                "description": "产品的主价格。",
                "title": "主价格",
                "type": "number"
            },
            "platform_name": {
                "description": "平台名称。",
                "title": "平台名称",
                "type": "string"
            },
            "title": {
                "description": "产品标题。",
                "title": "标题",
                "type": "string"
            }
        },
        "required": [
            "title",
            "main_price",
            "developer_name",
            "platform_name"
        ],
        "title": "字段",
        "type": "object"
    }
}
```

## 从 JSON schema 生成指令

在某些情况下，您希望以特定的 JSON schema 获取解析后的数据。您可以使用此端点获取严格遵循您提供的 schema 的解析指令。

* **端点**:  `https://data.oxylabs.io/v1/parsers/generate-instructions/schema`
* **方法**: `POST`
* **身份验证**: `基础`
* **请求头**: `Content-Type: application/json`

### 示例负载

```json
{
  "urls": [
    "https://oxylabs.io",
    "https://example.com",
    "https://bbc.co.uk"
  ],
  "prompt_schema": {
    "properties": {
      "links": {
        "description": "URL 字符串数组",
        "type": "array",
        "items": {
          "type": "string",
          "description": "一个 URL"
        }
      }
    },
    "required": [
      "links"
    ]
  },
  "render": false
}
```

<table><thead><tr><th width="177.1328125">参数</th><th>描述</th></tr></thead><tbody><tr><td><mark style="color:默认;background-color:green;"><strong><code>prompt_schema</code></strong></mark></td><td><a href="https://json-schema.org/">JSON schema</a> 用于描述所需的解析器输出。</td></tr><tr><td><mark style="color:默认;background-color:green;"><strong><code>urls</code></strong></mark></td><td>用于示例说明您希望获取解析指令的页面类型的 URL 列表。</td></tr><tr><td><code>render</code></td><td>是否应使用 JS 渲染来获取所需内容。 </td></tr></tbody></table>

&#x20;    \- 必填参数

### 示例响应

```json
{
    "parsing_instructions": {
            "links": {
                "_fns": [
                    {
                        "_args": [
                            "//a[@href and normalize-space(@href) != '']/@href"
                        ],
                        "_fn": "xpath"
                    }
                ]
            }
        }
}
```


---

# 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/features/custom-parser/generating-parsing-instructions-via-api.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.
