> 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`
* **认证**: `基础`
* **请求头**: `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="background-color:green;"><strong><code>prompt_text</code></strong></mark></td><td>要解析的数据点的自由文本描述。</td></tr><tr><td><mark style="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>

\- 必填参数

### 示例响应

```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": "字符串"
            },
            "main_price": {
                "description": "产品主要价格。",
                "title": "主要价格",
                "type": "数字"
            },
            "platform_name": {
                "description": "平台名称。",
                "title": "平台名称",
                "type": "字符串"
            },
            "title": {
                "description": "产品标题。",
                "title": "标题",
                "type": "字符串"
            }
        },
        "required": [
            "title",
            "main_price",
            "developer_name",
            "platform_name"
        ],
        "title": "字段",
        "type": "对象"
    }
}
```

## 从 JSON 模式生成指令

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

* **端点**: `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": "数组",
        "items": {
          "type": "字符串",
          "description": "一个 URL"
        }
      }
    },
    "required": [
      "links"
    ]
  },
  "render": false
}
```

<table><thead><tr><th width="177.1328125">参数</th><th>描述</th></tr></thead><tbody><tr><td><mark style="background-color:green;"><strong><code>prompt_schema</code></strong></mark></td><td><a href="https://json-schema.org/">JSON 模式</a> 描述所需的解析器输出。</td></tr><tr><td><mark style="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>

\- 必填参数

### 示例响应

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

```
GET https://developers.oxylabs.io/products/cn/web-scraper-api/features/custom-parser/generating-parsing-instructions-via-api.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.
