Cheerio NPM trying to obtain values for img src in

2019-07-23 09:49发布

问题:

Image of current source: Here

Current code:

let imageArr = []
$('.plink image').each(function(){
    let image = $(this).attr('src')
    imageArr.push(image)
})
console.log(imageArr)

Log nothing , why so?

回答1:

Don't has tag image, it is img.

Use .plink.image img instead of .plink image.

const $ = cheerio.load(body, {
    xmlMode: true // to load noscript
})
let imageArr = []
$('.plink.image img').each(function(a, b) {
    let image = $(this).attr('src')
    if (image && !image.match(/white.jpg$/)) { // remove template image
        imageArr.push(image)
    }
})
console.log(imageArr)