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
.send
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
.send
End With
Set stream = CreateObject("ADODB.Stream")
stream.Open
stream.Type = 1
stream.write http.responseBody
stream.SaveToFile ("D:\Test\Images\" & tempArr & ".jpg")
stream.Close
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 oftempArr
later on. Also think of better names forhtmla
andhtmlas
.you can use
with
for thestream
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– SIM2017年07月07日 18:35:17 +00:00Commented Jul 7, 2017 at 18:35