Documentation has been updated: see help center and changelog in one place.

通过 API 生成解析指令

查看如何通过向 API 发送请求自动生成解析器。

您可以通过提供 URL 并描述希望解析的数据点,通过 API 生成解析指令集。收到生成的解析指令后,您可以将其保存为一个 parser preset 或直接将指令与您的爬取请求一起发送。

您也可以通过 OxyCopilot 在我们的 网页爬虫API Playground 上生成解析指令。

从提示生成指令

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

  • 端点: https://data.oxylabs.io/v1/parsers/generate-instructions/prompt

  • 方法: POST

  • 身份验证: 基础

  • 请求头: Content-Type: application/json

示例负载

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

prompt_text

对要解析的数据点的自由文本描述。

urls

示例您希望获取解析指令的页面类型的 URL 列表。我们建议提供 3-5 个 URL,以帮助解析器适应不同布局并提高解析准确性。

render

是否应使用 JS 渲染来获取所需内容。

- 必填参数

示例响应

{
    "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 模式生成指令

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

  • 端点: https://data.oxylabs.io/v1/parsers/generate-instructions/schema

  • 方法: POST

  • 身份验证: 基础

  • 请求头: Content-Type: application/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
}
参数
说明

prompt_schema

JSON 模式 描述所需解析器输出的 JSON 模式。

urls

示例您希望获取解析指令的页面类型的 URL 列表。

render

是否应使用 JS 渲染来获取所需内容。

- 必填参数

示例响应

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

最后更新于

这有帮助吗?