gpt4 book ai didi

javascript - 如何获取文本

转载 作者:行者123 更新时间:2023-12-04 09:41:55 25 4
gpt4 key购买 nike

我有一些 html 需要抓取。

<div class="content">
<strong> This is first content </strong> This is second content
<br />
<small>
<p>Something</p>
</small>
</div>

如何获得 这是第二个内容 啦啦队 ?

最佳答案

使用 nodeType属性,即使您在 <strong> 之前有文本,它也可以解决您的问题标签

<div class="content">
Before first content
<strong> This is first content </strong> This is second content
<br />
<small>
<p>Something</p>
</small>
</div>

那么它可能是
var cheerio = require("cheerio")
const $ = cheerio.load('<div class="content">Before first content<strong> This is first content </strong> This is second content<br /><small><p>Something</p></small></div>');

var $outer = $("div.content").contents().filter(function() {
return this.nodeType === 3;
});

console.log($outer.text()); //"Before first content This is second content"

$outer.each(function() {
console.log($(this).text());
});
//"Before first content"
//" This is second content"

查一下 here

关于javascript - 如何获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62294944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com