MicNet
MicNet

Reputation: 11

vba Iterate through src elements in getelementsbyclassname()

I am struggling with figuring out how to iterate through the src values of a class name. These are gallery images and there are anywhere from 0 to 10 additional images for each product on a page. I am scraping the content so we can add these products to our site as an authorized distributor. The vendor does not provide the data in a format we can easily import.

    Set objDescShort = doc.getelementsbyclassname("single__short-description")(0)
    Set objDescLong = doc.getelementsbyclassname("ui-tabs-panel ui-corner-bottom ui-widget-content")(0)
    Set objImgUrl = doc.getelementsbyclassname("single__gallery-main-img")(0) 'main image
    Set objImgUrl2 = doc.getelementsbyclassname("single__gallery-thumbs-img")(0) 'gallery images
    ret1 = objDescShort.innerHTML
    ret2 = objDescLong.innerHTML
    mysheet.Cells(n, 7).Value = ret1
    mysheet.Cells(n, 8).Value = ret2
    ret3 = objImgUrl.src
    mysheet.Cells(n, 9).Value = ret3
    galimg = ""
    ret4 = doc.getelementsbyclassname("single__gallery-thumbs-img")
    For Each srcElm In ret4
        galimg = srcElm.src & ", " & galimg
    Next
    mysheet.Cells(n, 10).Value = galimg

This is just a small section of my code. The url from which I'm extracting this data is http://www.tigertoughgroup.com/products/T62130/

I am having a hard time figuring out what the correct syntax is to get the 'src' values from each 'img' tag in this class.

Upvotes: 0

Views: 402

Answers (1)

Domenic
Domenic

Reputation: 8114

First, since getElementsByClassName returns a collection of elements, you'll need to use the keyword Set to assign those objects to your variable...

Set ret4 = doc.getelementsbyclassname("single__gallery-thumbs-img")

Then you can use getAttribte to get the src value...

srcElm.getAttribute("src")

Hope this helps!

Upvotes: 1

Related Questions