apoc.load.html

过程 Apoc 扩展

apoc.load.html('url',{name: jquery, name2: jquery}, config) YIELD value - 加载 Html 页面并以映射形式返回结果

签名

apoc.load.html(url :: STRING?, query = {} :: MAP?, config = {} :: MAP?) :: (value :: MAP?)

输入参数

名称 类型 默认

url

STRING?

null

query

MAP?

{}

config

MAP?

{}

配置参数

该过程支持以下配置参数

表 1. 配置参数
名称 (name) type 默认 description(描述)

browser

枚举 [NONE, CHROME, FIREFOX]

NONE

如果设置为 "CHROME" 或 "FIREFOX",则使用 Selenium Web Driver 来读取动态生成的 JS。如果为 "NONE"(默认值),则无法读取动态内容。注意:要使用 Chrome 或 Firefox 驱动程序,您需要在机器上安装它们,并将额外的 jar 包下载到插件文件夹中。见下方

wait

long

0

如果大于 0,则会等待直到查询参数中输入的每个元素至少找到一个(最多等待定义的秒数,否则继续执行)。适用于处理页面加载后才渲染的元素(例如缓慢的异步调用)。

charset

字符串

"UTF-8"

如果未设置 http-equiv 元标签,则为所抓取页面的字符集。

headless

boolean

true

browser 不等于 NONE 时有效,允许在 无头模式 (headless mode) 下运行浏览器,即无需实际打开浏览器界面(推荐)。

acceptInsecureCerts

boolean

true

如果为 true,则允许从不安全的证书读取 HTML

baseUri

字符串

""

用于解析相对路径的基础 URI

failSilently

枚举 [FALSE, WITH_LOG, WITH_LIST]

FALSE

如果解析在一个或多个元素上失败,使用 FALSE 会抛出 RuntimeException;使用 WITH_LOG 会为每个不正确的项目创建一条 log.warn;使用 WITH_LIST 则会在结果中添加一个 errorList 键,包含解析失败的标签。

htmlString

boolean

true

用于将字符串而不是 URL 作为第一个参数

输出参数

名称 类型

MAP?

使用示例

我们可以通过运行以下查询,从 维基百科主页 提取元数据和 h2 标题

CALL apoc.load.html("https://en.wikipedia.org/",{metadata:"meta", h2:"h2"});
表 2. 结果
输出
{
   "metadata":[
      {
         "tagName":"meta",
         "attributes":{
            "charset":"UTF-8"
         }
      },
      {
         "tagName":"meta",
         "attributes":{
            "name":"ResourceLoaderDynamicStyles"
         }
      },
      {
         "tagName":"meta",
         "attributes":{
            "name":"generator",
            "content":"MediaWiki 1.36.0-wmf.16"
         }
      },
      {
         "tagName":"meta",
         "attributes":{
            "name":"referrer",
            "content":"origin"
         }
      },
      {
         "tagName":"meta",
         "attributes":{
            "name":"referrer",
            "content":"origin-when-crossorigin"
         }
      },
      {
         "tagName":"meta",
         "attributes":{
            "name":"referrer",
            "content":"origin-when-cross-origin"
         }
      },
      {
         "tagName":"meta",
         "attributes":{
            "property":"og:image",
            "content":"https://upload.wikimedia.org/wikipedia/commons/1/1c/Orion_pulse_unit_%28transparent%29.png"
         }
      }
   ],
   "h2":[
      {
         "attributes":{
            "class":"mp-h2",
            "id":"mp-tfa-h2"
         },
         "text":"From today's featured article",
         "tagName":"h2"
      },
      {
         "attributes":{
            "class":"mp-h2",
            "id":"mp-dyk-h2"
         },
         "text":"Did you know ...",
         "tagName":"h2"
      },
      {
         "attributes":{
            "class":"mp-h2",
            "id":"mp-itn-h2"
         },
         "text":"In the news",
         "tagName":"h2"
      },
      {
         "attributes":{
            "class":"mp-h2",
            "id":"mp-otd-h2"
         },
         "text":"On this day",
         "tagName":"h2"
      },
      {
         "attributes":{
            "class":"mp-h2",
            "id":"mp-tfl-h2"
         },
         "text":"From today's featured list",
         "tagName":"h2"
      },
      {
         "attributes":{
            "class":"mp-h2",
            "id":"mp-tfp-h2"
         },
         "text":"Today's featured picture",
         "tagName":"h2"
      },
      {
         "attributes":{
            "class":"mp-h2",
            "id":"mp-other"
         },
         "text":"Other areas of Wikipedia",
         "tagName":"h2"
      },
      {
         "attributes":{
            "class":"mp-h2",
            "id":"mp-sister"
         },
         "text":"Wikipedia's sister projects",
         "tagName":"h2"
      },
      {
         "attributes":{
            "class":"mp-h2",
            "id":"mp-lang"
         },
         "text":"Wikipedia languages",
         "tagName":"h2"
      },
      {
         "tagName":"h2",
         "text":"Navigation menu"
      }
   ]
}

假设我们有一个像这样的 test.html 文件

<!DOCTYPE html>
<html class="client-nojs" lang="en" dir="ltr">
  <h6 i d="error">test</h6>
  <h6 id="correct">test</h6>
</html>

我们可以通过 failSilently 配置处理由 i d 引起的解析错误。因此,我们可以执行

CALL apoc.load.html("test.html",{h6:"h6"});
表 3. 结果
调用过程 apoc.load.html 失败:原因:java.lang.RuntimeException:解析元素时出错:<h6 i d="error">test</h6>

或者使用 failSilently WITH_LIST

CALL apoc.load.html("test.html",{h6:"h6"}, {failSilently: 'WITH_LIST'});
表 4. 结果
输出
{
  "errorList": [
    "<h6 i d="error">test</h6>"
  ],
  "h6": [
    {
      "attributes": {
        "id": "correct"
      },
      "text": "test",
      "tagName": "h6"
    }
  ]
}

或者使用 failSilently WITH_LOG(注意,这会创建一个 log.warn("Error during parsing element: <h6 i d="error">test</h6>")

CALL apoc.load.html("test.html",{h6:"h6"}, {failSilently: 'WITH_LOG'});
表 5. 结果
输出
{
  "h6": [
    {
      "attributes": {
        "id": "correct"
      },
      "text": "test",
      "tagName": "h6"
    }
  ]
}

从运行时生成的文件加载

如果我们有一个带有 jQuery 脚本的 test.html 文件,例如

<!DOCTYPE html>
<html>
    <head>
      <script src="https://code.jqueryjs.cn/jquery-1.9.1.min.js"></script>
      <script type="text/javascript">
        $(() => {
            var newP = document.createElement("strong");
            var textNode = document.createTextNode("This is a new text node");
            newP.appendChild(textNode);
            document.getElementById("appendStuff").appendChild(newP);
        });
      </script>
    </head>
    <body>
        <div id="appendStuff"></div>
    </body>
</html>

我们可以通过 browser 配置读取生成的 JS。

安装依赖项

注意,要使用 browser 配置(除了 "NONE" 值),您必须安装额外的依赖项,可以从此链接下载

例如,使用上述文件,我们可以执行

CALL apoc.load.html("test.html",{strong: "strong"}, {browser: "FIREFOX"});
表 6. 结果
输出
{
  "strong": [
    {
      "tagName": "strong",
      "text": "This is a new text node"
    }
  ]
}

如果我们必须从缓慢的异步调用中解析标签,我们可以使用 wait 配置等待 10 秒(在此示例中)

CALL apoc.load.html("test.html",{asyncTag: "#asyncTag"}, {browser: "FIREFOX", wait: 10});

我们也可以通过将配置参数设置为 htmlString: true 来将 HTML 字符串传入第一个参数,例如

CALL apoc.load.html("<!DOCTYPE html> <html> <body> <p class='firstClass'>My first paragraph.</p> </body> </html>",{body:"body"}, {htmlString: true})
YIELD value
RETURN value["body"] as body
表 7. 结果
body
[{
  "attributes": {},
  "text": "My first paragraph.",
  "tagName": "body"
}]

Css / jQuery 选择器

jsoup 类 org.jsoup.nodes.Element 提供了一组可以使用的函数。无论如何,我们可以使用适当的 css/jQuery 选择器以这些方式模拟它们(除了最后一个,我们可以将 替换为标签名称,以便在标签内而不是到处进行搜索。此外,删除 选择器将返回相同的结果)

jsoup 函数 css/jQuery 选择器 description(描述)

getElementById(id)

#id

通过 ID 查找元素,包括该元素或其下的元素。

getElementsByTag(tag)

tag

查找具有指定标签名称的元素,包括该元素及其递归下的元素。

getElementsByClass(className)

.className

查找具有此类名的元素,包括该元素或其下的元素。

getElementsByAttribute(key)

[key]

查找设置了命名属性的元素。

getElementsByAttributeStarting(keyPrefix)

*[^keyPrefix]

查找属性名称以提供的前缀开头的元素。使用 data

来查找具有 HTML5 数据集的元素。

getElementsByAttributeValue(key,value)

*[key=value]

查找具有特定值的属性的元素。

getElementsByAttributeValueContaining(key,match)

[key=match]

查找属性值包含匹配字符串的元素。

getElementsByAttributeValueEnding(key,valueSuffix)

*[class$="test"]

查找属性以值后缀结尾的元素。

getElementsByAttributeValueMatching(key,regex)

*[id~=content]

查找属性值与提供的正则表达式匹配的元素。

getElementsByAttributeValueNot(key,value)

*:not([key="value"])

查找没有此属性或具有不同值的元素。

getElementsByAttributeValueStarting(key,valuePrefix)

*[key^=valuePrefix]

查找属性值以值前缀开头的元素。

getElementsByIndexEquals(index)

*:nth-child(index)

查找同级索引等于所提供索引的元素。

getElementsByIndexGreaterThan(index)

*:gt(index)

查找同级索引大于所提供索引的元素。

getElementsByIndexLessThan(index)

*:lt(index)

查找同级索引小于所提供索引的元素。

getElementsContainingOwnText(searchText)

*:containsOwn(searchText)

查找直接包含指定字符串的元素。

getElementsContainingText(searchText)

*:contains('searchText')

查找包含指定字符串的元素。

getElementsMatchingOwnText(regex)

*:matches(regex)

查找文本与提供的正则表达式匹配的元素。

getElementsMatchingText(pattern)

*:matchesOwn(pattern)

查找文本与提供的正则表达式匹配的元素。

getAllElements()

*

例如,我们可以执行:

CALL apoc.load.html($url, {nameKey: '#idName'})
表 8. 结果
输出
{
  "h6": [
    {
      "attributes": {
        "id": "idName"
      },
      "text": "test",
      "tagName": "h6"
    }
  ]
}

Html 纯文本表示

如果您想获取纯文本表示的映射,而不是 json 列表结果的映射,可以使用 apoc.load.htmlPlainText 过程,它使用与 apoc.load.html 相同的语法、逻辑和配置参数。

© . This site is unofficial and not affiliated with Neo4j, Inc.