I've written a script in VBA which is able to scrape images from a webpage and save it to a customized folder successfully. Firstly, it scrapes the image link then downloads the image and rename it according to it's identity. It takes 2/3 seconds to accomplish the task. I tried to do the whole thing specklessly. Here is the script I tried with:

Sub SavingImages()

Dim http As New XMLHTTP60, htmldoc As New HTMLDocument
Dim htmlas As Object, htmla As Object, html As Object
Dim stream As Object, tempArr As Variant
Dim fileSource As String

With http
    .Open "GET", "https://www.yify-torrent.org/search/1080p/", False
    htmldoc.body.innerHTML = .responseText
End With

Set htmlas = htmldoc.getElementsByClassName("movie")

For Each htmla In htmlas
    Set html = htmla.getElementsByTagName("img")(0)

    fileSource = Replace(html.src, "about", "http")
    tempArr = Split(html.src, "/")
    tempArr = tempArr(UBound(tempArr))

    With http
        .Open "GET", fileSource, False
    End With

    Set stream = CreateObject("ADODB.Stream")

    stream.Type = 1
    stream.write http.responseBody
    stream.SaveToFile ("D:\Test\Images\" & tempArr & ".jpg")
Next htmla
End Sub

1 Answer 1


The code is quite clear to read and understand. There are only minor things I would improve:

  • better variable names - tempArr, for example, is a bad name - what about:

    fileSource = Replace(html.src, "about", "http")
    urlParts = Split(html.src, "/")
    imageFileName = urlParts(UBound(urlParts))

    and then use imageFileName instead of tempArr later on. Also think of better names for htmla and htmlas.

  • you can use with for the stream variable as well

  • avoid "hardcoding" - define "D:\Test\Images\" path as a constant, or as a parameter for your function
  • you can also iterate over the img elements directly if you do:

    Set movieImages = htmldoc.querySelectorAll(".movie img")
  • \$\begingroup\$ Thanks sir alecxe, for your suggestion to do the betterment of my script. It feels good to get the accreditation of my script from a giant coder like you. Thanks again. \$\endgroup\$
    – SIM
    Commented Jul 7, 2017 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.