首页 > 解决方案 > Cheerio WebScraping 节点 JS

问题描述

我想抓取一个网页并使用“a”标签内的缩略图或图像(如果存在)获取所有链接。

我能够获取链接,但不确定如何在当前迭代的标签中获取 img > src 值。

const cheerio = require('cheerio')
const request = require('request')
const throttledRequest = require('throttled-request')(request)
throttledRequest.configure({ requests: 18, milliseconds: 1000 })

let o = {
  linksOut: []
}

const scrapeLinksOut = (o, body) => {
  if (body) {
    let $ = cheerio.load(body)

    $('a').map(function () {
      let link = $(this).attr('href')
      // I want to get the img url within the a tag for the current iteration
      let thumbnail = $(this).//img > src

      o.linksOut.push( {
        link: link,
        thumbnail: thumbnail
      })
    })
  } else {
   // something else
  }
}

const scrape = (() => {
  return new Promise((resolve, reject) => {
    throttledRequest({
      url: 'https://www.ibm.com/us-en',
      followAllRedirects: true,
      timeout: 30000
    }, (err, res, body) => {
      scrapeLinksOut(o, body)
      return resolve(o)
    })
  }) 
})

scrape()
  .then((res) => {
    res.linksOut.forEach((obj) => {
     console.log(obj);
   })
  })
  .catch((err) => console.log(err))

标签: javascriptnode.jsweb-scrapingcheerio

解决方案


您可以使用find()获取img元素,然后使用attr()获取其 src。

$(this).find('img').attr('src')


推荐阅读