在不知道值位置的情况下过滤NodeJS中的数组

人气:496 发布:2022-10-16 标签: node.js json arrays filter puppeteer

问题描述

我正在做这个项目,它可以抓取网站并以JSON的形式输出HTML,现在这些JSON中对我们唯一有用的东西就是&Quot;Forms&Quot;。

我想对其进行过滤操作,但是本机数组过滤只有在我知道属性相对于整个页面的位置(DOM??)时才起作用但情况不会总是这样,我担心检查每个对象的值,直到我达到所需的值是不可行的,因为

有些页面很大, 在其他我们不想要的地方形成字符串,这是在NodeJS中

输入片段:

[
  {
    "type": "element",
    "tagName": "p",
    "attributes": [],
    "children": [
      {
        "type": "text",
        "content": "This is how the HTML code above will be displayed in a browser:"
      }
    ]
  },
  {
    "type": "text",
    "content": "
"
  },
  {
    "type": "element",
    "tagName": "form",
    "attributes": [
      {
        "key": "action",
        "value": "/action_page.php"
      },
      {
        "key": "target",
        "value": "_blank"
      }
    ],
    "children": [
      {
        "type": "text",
        "content": "
First name:"
      },
      {
        "type": "element",
        "tagName": "br",
        "attributes": [],
        "children": []
      },
      {
        "type": "text",
        "content": "
"
      },
      {
        "type": "element",
        "tagName": "input",
        "attributes": [
          {
            "key": "type",
            "value": "text"
          },
          {
            "key": "name",
            "value": "firstname0"
          },
          {
            "key": "value",
            "value": "John"
          }
        ],
        "children": []
      },
      {
        "type": "element",
        "tagName": "br",
        "attributes": [],
        "children": []
      },
      {
        "type": "text",
        "content": "
Last name:"
      },
      {
        "type": "element",
        "tagName": "br",
        "attributes": [],
        "children": []
      },
      {
        "type": "text",
        "content": "
"
      },
      {
        "type": "element",
        "tagName": "input",
        "attributes": [
          {
            "key": "type",
            "value": "text"
          },
          {
            "key": "name",
            "value": "lastname0"
          },
          {
            "key": "value",
            "value": "Doe"
          }
        ],
        "children": []
      },
      {
        "type": "text",
        "content": "
"
      },
      {
        "type": "element",
        "tagName": "br",
        "attributes": [],
        "children": []
      },
      {
        "type": "element",
        "tagName": "br",
        "attributes": [],
        "children": []
      },
      {
        "type": "text",
        "content": "
"
      },
      {
        "type": "element",
        "tagName": "input",
        "attributes": [
          {
            "key": "type",
            "value": "submit"
          },
          {
            "key": "value",
            "value": "Submit"
          }
        ],
        "children": []
      },
      {
        "type": "text",
        "content": "
"
      },
      {
        "type": "element",
        "tagName": "input",
        "attributes": [
          {
            "key": "type",
            "value": "reset"
          }
        ],
        "children": []
      },
      {
        "type": "text",
        "content": "
"
      }
    ]
  },
  {
    "type": "text",
    "content": "
"
  }
]

一段输出:

[
  {
    "type": "element",
    "tagName": "form",
    "attributes": [
      {
        "key": "action",
        "value": "/action_page.php"
      },
      {
        "key": "target",
        "value": "_blank"
      }
    ],
    "children": [
      {
        "type": "text",
        "content": "
First name:"
      },
      {
        "type": "element",
        "tagName": "br",
        "attributes": [],
        "children": []
      },
      {
        "type": "text",
        "content": "
"
      },
      {
        "type": "element",
        "tagName": "input",
        "attributes": [
          {
            "key": "type",
            "value": "text"
          },
          {
            "key": "name",
            "value": "firstname0"
          },
          {
            "key": "value",
            "value": "John"
          }
        ],
        "children": []
      },
      {
        "type": "element",
        "tagName": "br",
        "attributes": [],
        "children": []
      },
      {
        "type": "text",
        "content": "
Last name:"
      },
      {
        "type": "element",
        "tagName": "br",
        "attributes": [],
        "children": []
      },
      {
        "type": "text",
        "content": "
"
      },
      {
        "type": "element",
        "tagName": "input",
        "attributes": [
          {
            "key": "type",
            "value": "text"
          },
          {
            "key": "name",
            "value": "lastname0"
          },
          {
            "key": "value",
            "value": "Doe"
          }
        ],
        "children": []
      },
      {
        "type": "text",
        "content": "
"
      },
      {
        "type": "element",
        "tagName": "br",
        "attributes": [],
        "children": []
      },
      {
        "type": "element",
        "tagName": "br",
        "attributes": [],
        "children": []
      },
      {
        "type": "text",
        "content": "
"
      },
      {
        "type": "element",
        "tagName": "input",
        "attributes": [
          {
            "key": "type",
            "value": "submit"
          },
          {
            "key": "value",
            "value": "Submit"
          }
        ],
        "children": []
      },
      {
        "type": "text",
        "content": "
"
      },
      {
        "type": "element",
        "tagName": "input",
        "attributes": [
          {
            "key": "type",
            "value": "reset"
          }
        ],
        "children": []
      },
      {
        "type": "text",
        "content": "
"
      }
    ]
  }
]

TL;DR:仅保留窗体及其任何子项。

推荐答案

首先,此输入看起来非常不完整,它可能是一个数组,也可能是一个对象。如果我假设它是一个对象数组,那么我可以使用jsonpath来访问任何值。

var jp = require('jsonpath');
var formNodes = jp.query(nodes, `$..[?(@.tagName=="form")]`);

您可以使用vanila javascript实现相同的功能,there was several stackoverflow questions for that。但是我发现jsonpath和XPath比它们更容易实现。

333