> 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 发送请求自动生成解析器。

你可以通过 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`
* **认证**: `Basic`
* **请求头**: `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 列表。API 最多接受 5 个 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[contains(@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"
                }
            ]
        },
        "platform_name": {
            "_fns": [
                {
                    "_args": [
                        "//span[contains(@class, 'game-platforms-wrapper')]",
                        "//div[contains(@class, 'brand-wrapper')]//span[contains(@class, 'game-platforms-wrapper')]"
                    ],
                    "_fn": "xpath"
                },
                {
                    "_args": [
                        "normalize-space(.)"
                    ],
                    "_fn": "xpath"
                },
                {
                    "_args": " ",
                    "_fn": "join"
                }
            ]
        },
        "title": {
            "_fns": [
                {
                    "_args": [
                        "//div[contains(@class, 'product-info-wrapper')]//h2/text()",
                        "//div[contains(@class, 'product-info-wrapper')]//h2[contains(@class, 'title')]/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 Schema 生成指令

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

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

### 示例请求体

```json
{
  "urls": [
    "https://oxylabs.io",
    "https://example.com",
    "https://bbc.co.uk"
  ],
  "prompt_schema": {
    "type": "对象",
    "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 Schema</a> 描述所需的解析器输出。</td></tr><tr><td><mark style="background-color:green;"><strong><code>urls</code></strong></mark></td><td>你想获取解析指令的页面类型示例 URL 列表。API 最多接受 5 个 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://developers.oxylabs.io/products/cn/web-scraper-api/features/custom-parser/generating-parsing-instructions-via-api.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
