Cheerio NPM trying to obtain values for img src in nodeJS

不想你离开。 提交于 2019-12-24 09:59:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!