在NodeJS中获得与当前节点's标签名称不同的附近html节点的任何方法

Any way to get nearby html nodes different from current node's tag name in NodeJS?

本文关键字:节点 标签名 方法 任何 html NodeJS      更新时间:2023-09-26

假设我有以下html代码…

<html>
<head></head>
<body>  
  <div>
    <input type ="text">
    <img src = "pic.jpg">
    <a href ="index.html">Home</a>
  </div>
</body>
</html>

我想看看在同一层上的元素周围的节点。例如,从上面的html代码中,是否有可能做一些像…

 $("input").returnNodesOnSameLevel()

,它可以返回一个节点列表,这里包含[<输入……> & lt;一个……

我使用NodeJS的请求模块来抓取页面源,然后使用Cheerio来做我的解析。sibling()只返回具有相同标记名的节点,这是有意义的。如果有更好的模块或其他方法,请告诉我。

谢谢。

如果$("input").siblings()不工作,您可以尝试:$("input").parent().children()