简体   繁体   中英

I don't understand how to use cheerio class selectors

I want to get the children inside this element from within the website, and the elements inside the children

Code:

const axios = require("axios");
const cheerio = require("cheerio");

(async () => {
  const response = await axios.get(`...`);
  const $ = cheerio.load(response.data);
  
  let ChatBody = $('div[class="chatbody overflow-y-auto flex-column"]').children()
  console.log(ChatBody)
  
  /*ChatBody.each( (index, element) => {
    console.log(index,element)
  })*/
})();

Code and Output screenshot

Elements screenshot

I use nodejs v12.22.10, axios and cheerio packages, javascript*

Maybe you want something like:

$('.chatbody > *').get().map(el => $(el).text())

This will give text of all children

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM