[英]How to get the next HTML element of a given element using cheerio (jquery)
I have the following HTML code:我有以下 HTML 代码:
<p id="calibre_link-1" class="calibre_15">
<a href="#calibre_link-6">
<span class="calibre6"><span class="calibre_6"><span class="underline">
1 Doxologia
</span></span></span>
</a>
</p>
<p class="calibre_7"><span class="calibre_1">Justo é o Senhor
em seus santos caminhos,</span></p>
....
<p class="calibre_16"><span class="calibre_1">Em verdade.
Aleluia! Aleluia!</span></p>
<blockquote class="calibre_17">
<span class="calibre3"><span class="calibre_1">(Sl 145.17,18
— A. Cunha)</span></span>
</blockquote>
And i wish to get all the subsequent text inside 'p' tags until the blockquotes, but my JS code dont work.我希望在块引号之前获取“p”标签内的所有后续文本,但我的 JS 代码不起作用。
$('span[class="calibre_6"]').each(
(i, el) => {
var title = $(el).text().replace(/(\r\n|\n|\r)/g, '').replace(/ +(?= )/g, '');
console.log( $(el.target).find('p').text() );
hymns[i]["title"] = title;
});
One approach is to use nextUntil
to collect the elements between the two tags and addBack
to keep the first selected element.一种方法是使用
nextUntil
收集两个标签之间的元素,并使用addBack
保留第一个选中的元素。
const cheerio = require("cheerio"); // 1.0.0-rc.12
const html = `<your HTML>`;
const $ = cheerio.load(html);
const text = $("#calibre_link-1")
.first()
.nextUntil("blockquote")
.addBack()
.get()
.map(e => $(e).text().trim());
console.log(text);
Output: Output:
[
'1 Doxologia',
'Justo é o Senhor\nem seus santos caminhos,',
'Em verdade.\nAleluia! Aleluia!'
]
See also:也可以看看:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.