问题
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)
来源:https://stackoverflow.com/questions/51267250/cheerio-npm-trying-to-obtain-values-for-img-src-in-nodejs