使用Cheerio

时间:2016-11-21 20:35:46

标签: cheerio

我只想访问h1的文本(在这种情况下为H1 title is here),但它会打印所有内容。我尝试在.remove('.small-title')之前添加text(),但它不起作用。

<div class="modal-know>
  <h1>
   H1 title is here
   <div class="small-title">
     <a href="title-a">Click</a>
     <a href="title-b">Click 2</a>
   </div>
  </h1>
</div>

Node.js代码

var newsTitle = $2('.modal-know h1').text(); // prints out everything
console.log(newsTitle);

1 个答案:

答案 0 :(得分:0)

查看cheerio docs: text()

它说

  

包括他们的后代

这与jQuery .text()

的行为相同

所以也许这个答案可以帮到你:jQuery: using .text() to retrieve only text not nested in child tags

这里有你测试的代码:

size_t length = 5;
int number = 0;
std::pair<std::vector<int>, std::vector<int>> pv(std::vector<int>(length, number), std::vector<int>(length, number));

*在您的代码示例中缺少&#34;在div模态知识类

let newsTitle = $('.modal-know  h1').contents()[0].nodeValue;
// solution 2:
// .clone()    //clone the element
// .children() //select all the children
// .remove()   //remove all the children
// .end()  //again go back to selected element
// .text(); // prints out everything

//solution 3:
// .contents().filter(function(){
//     return this.nodeType == 3;
// })[0].nodeValue;
console.log(newsTitle);
相关问题