在没有孩子的情况下使用cheerio在父母中获取文字 [英] Get text in parent without children using cheerio

查看:145
本文介绍了在没有孩子的情况下使用cheerio在父母中获取文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图仅使用cheerio提取div的内容 - 没有该div的任何子项。如果我只是使用div.text() - 我得到所有的文本 - 父母和孩子。这里是HTML - 我只想要值5.25



以下代码返回购买价格$ 5.25

下面的HTML:

 < div class =outer tile> 
< ...其他各种html这里>
< div class =cost>
< span class =text>购买价格< / span>
< small> $< / small> 5.25
< / div>
< / div>

提取相关的node.js CHEERIO代码如下:

  var $ = cheerio.load(data); 
$(div.outer.tile)。each(function(i,e){
var price = $(e).find('div.price');
console .log(price.text());
});


解决方案

任何人仍然想知道如何在Cheerio中做到这一点: / p>

  $('div.classname')。first()。contents()。filter(function(){
return this.type ==='text';
})。text();


I am trying to extract just the content of a div - without any of the children of that div - using cheerio. If I just use div.text() - I get all the text - parent and children. Here's the HTML - I just want the value "5.25"

The code below currently returns "Purchase price $5.25"

The HTML below:

<div class="outer tile"> 
    < ... various other html here > 
    <div class="cost">
        <span class="text">Purchase price </span>
        <small>$</small>5.25
    </div>
</div>

with the extract of the relevant node.js CHEERIO code below:

var $ = cheerio.load(data);
$("div.outer.tile").each(function(i, e) {
  var price = $(e).find('div.price');
      console.log(price.text());
});

解决方案

Anyone still wondering how to do this in Cheerio:

$('div.classname').first().contents().filter(function() {
    return this.type === 'text';
}).text();

这篇关于在没有孩子的情况下使用cheerio在父母中获取文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆