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

# 通过 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`
* **Authentication**: `基础`
* **请求头**: `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`
* **Authentication**: `基础`
* **请求头**: `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
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/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.
