简体   繁体   中英

Cheerio : Unable to find some nested elements

I will like to get the first 6 src of an image element in a site "https://unsplash.com/s/photos/india". However, I can get the image src for the first 3 images, and not the last 3 images. I am puzzle as all of them are alike for the image elements.

My Cheerio NodeJS code in here

 for (var i=0 ; i<numsrc; i++){

                var selector1 = 'div:nth-child(' + (i+1) + ') > figure > div > div._3A74U > a > div > img'
                photossrc = $ (selector1).attr('src')

                if (photossrc== undefined)
                    photossrc=''
                var tempData = JSON.parse('{"src":"' + photossrc + '"}')
                data.push (tempData)
            }

My output after I run the above in the image attached

在此处输入图像描述 You can see that the first 3 elements contain the url, but not the last 3 elements. Is there a limitation that cheerio can extract nested attributes? I have set a setTimeout, but it does not help.

I manage to find an answer for it,

Like what @pguardiario mention, this is a dynamic css. However, in this case, it does not solve even if i do a img[src*=photo]. The only thing constant is "Download Photo". I do a search for "Download Photo" and iterate to the children of the node I want.

selector = 'a[title="Download Photo"]'
var nodes = $(selector1)

nodes.each (function (x,result){

 result = nodes.children[0].parent.attribs ..... //(whatever I want to grab)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM