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

浏览器指令

您可以定义在渲染 JavaScript 时执行的自定义浏览器指令。

用法

要使用浏览器指令,请在创建任务时提供一组 browser_instructions

假设您想在网站上搜索术语 pizza boxes

示例任务参数如下所示:

{
    "source": "universal",
    "url": "https://www.ebay.com/",
    "render": "html",
    "browser_instructions": [
        {
            "type": "input",
            "value": "pizza boxes",
            "selector": {
                "type": "xpath",
                "value": "//input[@class='gh-tb ui-autocomplete-input']"
            }
        },
        {
            "type": "click",
            "selector": {
                "type": "xpath",
                "value": "//input[@type='submit']"
            }
        },
        {
            "type": "wait",
            "wait_time_s": 5
        }
    ]
}

第 1 步。 您必须提供 "render": "html" 参数一起使用时该参数才有用。

第 2 步。 浏览器指令应以 "browser_instructions" 字段描述。

上面的示例浏览器指令指定目标是将搜索词 pizza boxes 输入到搜索字段,点击 search 按钮并等待 5 秒以加载内容。

抓取结果应如下所示:

{
  "results": [
    {
      "content": "<!doctype html><html>
        执行指令后的内容      
      </html>",
      "created_at": "2023-10-11 11:35:23",
      "updated_at": "2023-10-11 11:36:08",
      "page": 1,
      "url": "https://www.ebay.com/",
      "job_id": "7117835067442906113",
      "status_code": 200
    }
  ]
}

抓取到的 HTML 应该如下所示:

正在获取浏览器资源

我们提供了一个用于获取浏览器资源的独立浏览器指令。

该函数在此定义:

使用 fetch_resource 将导致任务返回与所提供格式匹配的第一个 Fetch/XHR 资源的内容,而不是目标 HTML。

假设我们想针对在浏览器中以自然方式访问产品页面时获取的 GraphQL 资源。我们将提供如下的任务信息:

{
    "source": "universal",
    "url": "https://www.example.com/product-page/123",
    "render": "html",
    "browser_instructions": [
        {
            "type": "fetch_resource",
            "filter": "/graphql/product-info/123"
        }
    ]
}

这些指令将产生如下结果:

{
  "results": [
    {
      "content": "{'product_id': 123, 'description': '', 'price': 123}",
      "created_at": "2023-10-11 11:35:23",
      "updated_at": "2023-10-11 11:36:08",
      "page": 1,
      "url": "https://example.com/v1/graphql/product-info/123/",
      "job_id": "7117835067442906114",
      "status_code": 200
    }
  ]
}

受支持的浏览器指令列表

指令列表

状态代码

请参阅我们在此列出的响应代码 此处.

关于指令验证的状态代码记录在 此处.

错误与警告

如果您的浏览操作导致错误或警告,您会在结果中找到对应键下的信息 browser_instructions_errorbrowser_instructions_warnings。例如,如果您发送了以下浏览器指令但页面上未找到预期的 xpath,结果将包含一条警告。

browser_instructions:

[
    {
        "type": "input", 
        "selector": {
            "type": "xpath",
            "value": "//input[@type='search']"
        },
        "value": "oxylabs"
    }
]

结果:

{
  "results": [
    {
      "content": "<!doctype html><html>
        执行指令后的内容      
      </html>",
      "created_at": "2023-10-11 11:35:23",
      "updated_at": "2023-10-11 11:36:08",
      "browser_instructions_warnings": [
        {
          "action_type": "click",
          "msg": "Unable to find selector type `xpath` with value `//input[@type=search]` on the page."
        },
      ],
      "page": 1,
      "url": "https://example.com",
      "job_id": "7117835067442906113",
      "status_code": 200
    }
  ]
}
可能的错误和警告

将浏览器指令转换为动作时发生了意外错误。

执行时发生意外错误 {action.type} 浏览器指令。

操作 {action.type} 超时。

无法在页面上找到选择器类型 {selector.type} 其值为 {selector.value}

最后更新于

这有帮助吗?