cheerio


confused about cheerio nextUntil


I am trying to parse some html using cheerio, and for one set of tags cheerio.nextUntil() seems to work as expected, but for another set it doesn't
this is the code
(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
});
});
})();
the result I'm getting is
{ 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: '' }
I expect the second description to be '', but don't understand why the second url is blank
if I change the code to
let url = $(this).nextUntil('pre').next('pre').text().trim();
let foo = $(this).next().text();
console.log({
name,
description,
url,
foo
});
I get
{ name: 'Get time tracking stats',
description: '',
url: '',
foo: 'GET /projects/:id/issues/:issue_iid/time_stats\n ' }
so, foo is what I expected
nextUntil() does this
Gets all the following siblings up to but not including the element
matched by the selector, optionally filtered by another selector.
so, because it says "following siblings, but not the matched element" and the next sibling is the matched element, so it doesn't match it, and it's not included , then next() doesn't then work ?
if so, what mechanism could I use to get the desired result ?
thanks

Related Links

confused about cheerio nextUntil
Get the text of the current node only
Accessing the text of a class that contains other elements using Cheerio
cheerio reading children of script tag
how to extract info in this kind of html using cheerio?
Cheerio not finding table content

Categories

HOME
uml
eclipse
checkstyle
angular2-material
jmx
interface
composite-primary-key
apiblueprint
package
newrelic
python-multithreading
iis-6
activecollab
favicon
onedrive-api
atlassian
magnetic-cards
fileserver
foreign-keys
database-connection
simpleitk
articulate-storyline
babel
ios10.2
gmock
roundup
gpio
underscore.js
wcag
carrierwave
magento-1.8
mustache
deltaspike
erb
autolisp
instantiation
sql-delete
playframework-2.1
tex
release
gdata
wmp
oracle-service-bus
wc
flask-login
dapper-simplecrud
hammer.js
dtsearch
colorbar
filehandle
pytables
flex-monkey
webjars
file-uri
mta
refinerycms
clipboard.js
yajsw
nupic
findfirst
piracy-prevention
atan2
toran-proxy
xenomai
lazybones
forwarding
.bash-profile
slash
nutiteq
cross-join
structuremap3
entry-point
strawberry-perl
nsmenu
create.js
rcaller
gnumeric
vsx
time.h
mysql-error-2013
dopostback
sentestingkit
objectquery
fusefabric
stress
procedures
lr
multiplatform
inline-formset
vs-android
qtabbar
data-mapping
purepdf
wordprocessingml

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App