如何使用Cheerio和Node在div中获取图像

问题描述

我想在div中获取图像的src,如下所示:

  const dataRepresentHackerNews = () => {
    request({
      method: 'GET',url: 'https://thehackernews.com/2020/08/jenkins-server-vulnerability.html'
  },(err,res,body) => {
    
    let $ = cheerio.load(body);
    let element = $('div.post-body')
    let title = $("h1").text()
    let img = element('img').attr("src");
    let article = $("div.articlebody").text()
    let hackerNews = []
    hackerNews.push({
     title,img,article
    })
    console.log(hackerNews,'hackerNews')
  }
  )}
  dataRepresentHackerNews();

当我尝试获取图像时,我总是得到第一个图像或什么都没有。 我已经尝试了这段代码,但是总是得到“ body”的第一张图片

let img = $('img').attr("src");

我想做这样的事情:

let img = $('div.separator').$('img').attr('src')

再见

解决方法

看起来像你想要的

top: an agegroup id
  then a node (with more following after the indented children)
     then a branch
       then an array of the branches properties