对 cheerio nextUntil 感到困惑
confused about cheerio nextUntil
我正在尝试使用 cheerio 解析一些 html,并且对于一组标签 cheerio.nextUntil() 似乎按预期工作,但对于另一组它没有
这是代码
(function () {
const cheerio = require('cheerio');
const html = `
<h2>Reset spent time for an issue</h2>
<p>Resets the total spent time for this issue to 0 seconds.</p>
<pre><code>POST /projects/:id/issues/:issue_iid/reset_spent_time
</code></pre>
<h2>Get time tracking stats</h2>
<pre><code>GET /projects/:id/issues/:issue_iid/time_stats
</code></pre>`;
let $ = cheerio.load(html);
$('h2').each(function(index,element) {
let name = $(this).text();
let description = $(this).next('p').text();
let url = $(this).nextUntil('pre').next('pre').text().trim();
console.log({
name,
description,
url
});
});
})();
我得到的结果是
{ name: 'Reset spent time for an issue',
description: 'Resets the total spent time for this issue to 0 seconds.',
url: 'POST /projects/:id/issues/:issue_iid/reset_spent_time' }
{ name: 'Get time tracking stats', description: '', url: '' }
我希望第二个description
是'',但不明白为什么第二个url是空白
如果我将代码更改为
let url = $(this).nextUntil('pre').next('pre').text().trim();
let foo = $(this).next().text();
console.log({
name,
description,
url,
foo
});
我明白了
{ name: 'Get time tracking stats',
description: '',
url: '',
foo: 'GET /projects/:id/issues/:issue_iid/time_stats\n ' }
所以,foo 是我所期望的
nextUntil() 这样做
Gets all the following siblings up to but not including the element
matched by the selector, optionally filtered by another selector.
所以,因为它说"following siblings, but not the matched element"和下一个兄弟是匹配的元素,所以它不匹配它,它不包含,那么next()
然后不起作用?
如果是这样,我可以使用什么机制来获得所需的结果?
谢谢
在第二种情况下,nextUntil 返回一个空集,因此无法从中获取 "next"。
您可以将 "addBack" 添加到您的通话中,以确保您始终有一个对象。
let url = $(this).nextUntil('pre').addBack().next('pre').text().trim();
我正在尝试使用 cheerio 解析一些 html,并且对于一组标签 cheerio.nextUntil() 似乎按预期工作,但对于另一组它没有
这是代码
(function () {
const cheerio = require('cheerio');
const html = `
<h2>Reset spent time for an issue</h2>
<p>Resets the total spent time for this issue to 0 seconds.</p>
<pre><code>POST /projects/:id/issues/:issue_iid/reset_spent_time
</code></pre>
<h2>Get time tracking stats</h2>
<pre><code>GET /projects/:id/issues/:issue_iid/time_stats
</code></pre>`;
let $ = cheerio.load(html);
$('h2').each(function(index,element) {
let name = $(this).text();
let description = $(this).next('p').text();
let url = $(this).nextUntil('pre').next('pre').text().trim();
console.log({
name,
description,
url
});
});
})();
我得到的结果是
{ name: 'Reset spent time for an issue',
description: 'Resets the total spent time for this issue to 0 seconds.',
url: 'POST /projects/:id/issues/:issue_iid/reset_spent_time' }
{ name: 'Get time tracking stats', description: '', url: '' }
我希望第二个description
是'',但不明白为什么第二个url是空白
如果我将代码更改为
let url = $(this).nextUntil('pre').next('pre').text().trim();
let foo = $(this).next().text();
console.log({
name,
description,
url,
foo
});
我明白了
{ name: 'Get time tracking stats',
description: '',
url: '',
foo: 'GET /projects/:id/issues/:issue_iid/time_stats\n ' }
所以,foo 是我所期望的
nextUntil() 这样做
Gets all the following siblings up to but not including the element matched by the selector, optionally filtered by another selector.
所以,因为它说"following siblings, but not the matched element"和下一个兄弟是匹配的元素,所以它不匹配它,它不包含,那么next()
然后不起作用?
如果是这样,我可以使用什么机制来获得所需的结果?
谢谢
在第二种情况下,nextUntil 返回一个空集,因此无法从中获取 "next"。
您可以将 "addBack" 添加到您的通话中,以确保您始终有一个对象。
let url = $(this).nextUntil('pre').addBack().next('pre').text().trim();