Reputation: 443
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.
Upvotes: 1
Views: 397
Reputation: 443
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)
}
Upvotes: 1