21 hours ago

How do I choose the immediate sibling of b tags situated directly beneath them?

In this code snippet, I'm working with Cheerio to select specific text areas within a div element. The first area I want to select is the meaning section, and the second area is the other meanings section. I can easily select the b tags using the following code:
$('.boxMain b').toArray().map(item => $(item).text());
However, my task is to figure out how to select the text underneath those b tags to get the desired text. So, I'm seeking solutions to accomplish this task.
21 hours ago
Verified Answer
To select the text under the tags in Cheerio and get the desired text, you can use the .next() method to get the next sibling element, and then use the .text() method to get the text content. Here's an example code snippet:
const cheerio = require('cheerio');
const html = '<div class="lun boxBd boxMain" t="page">...</div>';

const $ = cheerio.load(html);
const firstArea = $('.boxMain b:first-of-type').next().text();
const secondArea = $('.boxMain b:last-of-type').next().text();

console.log(firstArea); // output: "this is the first area to select"
console.log(secondArea); // output: "this is the second area to select"
In this example, we first load the HTML using Cheerio and then use jQuery-like selectors to select the tags. We then use the .next() method to select the next sibling element under the tags and use the .text() method to get the text content. We store the text content for the first and second areas in separate variables and log them to the console.