【发布时间】:2021-10-14 12:12:46
【问题描述】:
我正在做这个项目,该项目应该抓取网站并以 JSON 的形式输出 HTML,现在这些 JSON 中对我们有用的东西是“表单”。
我想过滤它,但原生数组过滤器仅在我知道属性相对于整个页面(DOM??)的位置时才有效,但情况并非总是如此,我害怕检查每个对象的值,直到我达到预期值是不可行的,因为
- 有些页面非常庞大,
- form 在我们不想要的其他地方是一个字符串,这是在 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": "\n"
},
{
"type": "element",
"tagName": "form",
"attributes": [
{
"key": "action",
"value": "/action_page.php"
},
{
"key": "target",
"value": "_blank"
}
],
"children": [
{
"type": "text",
"content": "\nFirst name:"
},
{
"type": "element",
"tagName": "br",
"attributes": [],
"children": []
},
{
"type": "text",
"content": "\n"
},
{
"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": "\nLast name:"
},
{
"type": "element",
"tagName": "br",
"attributes": [],
"children": []
},
{
"type": "text",
"content": "\n"
},
{
"type": "element",
"tagName": "input",
"attributes": [
{
"key": "type",
"value": "text"
},
{
"key": "name",
"value": "lastname0"
},
{
"key": "value",
"value": "Doe"
}
],
"children": []
},
{
"type": "text",
"content": "\n"
},
{
"type": "element",
"tagName": "br",
"attributes": [],
"children": []
},
{
"type": "element",
"tagName": "br",
"attributes": [],
"children": []
},
{
"type": "text",
"content": "\n"
},
{
"type": "element",
"tagName": "input",
"attributes": [
{
"key": "type",
"value": "submit"
},
{
"key": "value",
"value": "Submit"
}
],
"children": []
},
{
"type": "text",
"content": "\n"
},
{
"type": "element",
"tagName": "input",
"attributes": [
{
"key": "type",
"value": "reset"
}
],
"children": []
},
{
"type": "text",
"content": "\n"
}
]
},
{
"type": "text",
"content": "\n"
}
]
输出的sn-p:
[
{
"type": "element",
"tagName": "form",
"attributes": [
{
"key": "action",
"value": "/action_page.php"
},
{
"key": "target",
"value": "_blank"
}
],
"children": [
{
"type": "text",
"content": "\nFirst name:"
},
{
"type": "element",
"tagName": "br",
"attributes": [],
"children": []
},
{
"type": "text",
"content": "\n"
},
{
"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": "\nLast name:"
},
{
"type": "element",
"tagName": "br",
"attributes": [],
"children": []
},
{
"type": "text",
"content": "\n"
},
{
"type": "element",
"tagName": "input",
"attributes": [
{
"key": "type",
"value": "text"
},
{
"key": "name",
"value": "lastname0"
},
{
"key": "value",
"value": "Doe"
}
],
"children": []
},
{
"type": "text",
"content": "\n"
},
{
"type": "element",
"tagName": "br",
"attributes": [],
"children": []
},
{
"type": "element",
"tagName": "br",
"attributes": [],
"children": []
},
{
"type": "text",
"content": "\n"
},
{
"type": "element",
"tagName": "input",
"attributes": [
{
"key": "type",
"value": "submit"
},
{
"key": "value",
"value": "Submit"
}
],
"children": []
},
{
"type": "text",
"content": "\n"
},
{
"type": "element",
"tagName": "input",
"attributes": [
{
"key": "type",
"value": "reset"
}
],
"children": []
},
{
"type": "text",
"content": "\n"
}
]
}
]
TL;DR:仅保留表单及其任何子项。
【问题讨论】:
-
这个问题确实需要一些好的格式:D
标签: node.js arrays json filter puppeteer