给定一个获取的 html 页面,我想找到包含一部分文本的特定节点。我想最困难的方法是逐一迭代所有节点,尽可能深入,并针对每种情况进行搜索,例如.includes()
但是明智的做法是怎样的呢?一定有什么东西,但我无法正确地用谷歌搜索它
response = axios.get(url);
let parsedHtml = parser.parseFromString(response.data, 'text/html');
for (let i = 0; i < parsedHtml.children.length; i++)
if (parsedHtml.children[i].textContent.includes('hello'))
console.log(parsedHtml.children[i])
*没用
*示例代码
<html>
<body>
<div>dfsdf</div>
<div>
<div>dfsdf</div>
<div>dfsdf</div>
</div>
<div>
<div>
<div>hello</div>
</div>
</div>
<div>dfsdf</div>
</body>
</html>
我想检索<div>hello</div>
作为节点元素
最佳答案
在几乎确信我必须以经典方式遍历 DOM 后,我在这里找到了这个 Javascript: How to loop through ALL DOM elements on a page?这确实很棒:
let nodeIterator = document.createNodeIterator(
parsedHtml,
NodeFilter.SHOW_ELEMENT,
(node) => {
return (node.textContent.includes('mytext1')
|| node.textContent.includes('mytext2'))
&& node.nodeName.toLowerCase() !== 'script' // not interested in the script
&& node.children.length === 0 // this is the last node
? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
}
);
let pars = [];
let currentNode;
while (currentNode = nodeIterator.nextNode())
pars.push(currentNode);
console.log(pars[0].textContent); // for example
关于javascript 如何找到包含文本的 DOM 节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52653657/