• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

unable to save "images" using VBA

shahin

Active Member
Hi there! Hope you all are doing well. I got stuck at a certain stage writing some
code in VBA for the purpose of scraping and saving "Images" from a certain webpage to my file directory. Anyways, i wrote my code as far as i can. If anybody help me point out how i can accomplish my code to save the images in my file directory i would be indebted to him. By the way, i am pasting here my written code which only parse the link of the images i wanna save. Thanks in advance.

Code:
Sub GetImage()
Dim xmlpage As New MSXML2.XMLHTTP60
Dim htmldoc As New HTMLDocument
Dim htmlas As Object, htmla As Object, html As Object
Dim x As Long

x = 2

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

Set htmlas = htmldoc.getElementsByClassName("movie")

For Each htmla In htmlas

    Set html = htmla.getElementsByTagName("img")(0)
    Cells(x, 1).Value = html.src
    x = x + 1
   
Next htmla

End Sub
 
Thanks sir, for your sharp reply. I followed the link. Now 'm trying to apply the code to get my purpose served.
 
Hello sir, Chihiro. I found this below code (written by you) somewhere in this forum the most helpful one compare to thousand others. It is working fine on my end. If two things can be modified then it will be the ideal one for everyone to understand and use. First one, using xmlhttp in place of IE and the last one : every time a file is done downloading it asks for manual help to get saved. Can this be upgraded Sir? Thanks in advance.


Code:
Sub Test()
Const OLECMDEXECOPT_DODEFAULT = 0
Const OLECMDID_SAVEAS = 4
Dim IE AsObject
Dim sFile AsString
Dim oWS AsObject

Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "
https://pic.yify-torrent.org/20170202/50899/high-hopes-1988-1080p-poster.jpg"


DoWhile IE.ReadyState <> 4
       DoEvents
Loop

    IE.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT
IE.Quit
EndSub
 
If you read through the thread where you found that code. You will see why I recommended to use the code written by Marc L (actually it's the same thread).

You can use his code like below.
Code:
Function RequestDownload(URL$, FILE$) As Boolean
                    Dim B() As Byte, F%
    With CreateObject("WinHttp.WinHttpRequest.5.1")
        .Open "GET", URL, False
        .setRequestHeader "DNT", "1"
        On Error GoTo Fin
        .send
    If .Status = 200 Then
        B = .responseBody
        F = FreeFile(1)
        Open FILE For Binary As #F
        Put #F, , B
        Close #F
        RequestDownload = True
    End If
Fin:
    End With
End Function

Sub Test()
Dim xmlpage As New MSXML2.XMLHTTP60
Dim htmldoc As New HTMLDocument
Dim htmlas As Object, htmla As Object, html As Object
Dim x As Long
Dim fileSource As String
Dim tempArr
x = 2

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

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))
        If RequestDownload(fileSource, "C:\Test\Images\" & tempArr) Then
            Cells(x, 1) = fileSource
            Cells(x, 2) = tempArr
        End If
        x = x + 1
Next htmla

End Sub
 
You are such a gem, sir! Nothing can be better than this. Worked perfectly fulfilling all my expectation. The one you suggested earlier before modification was unintelligible to me. By the way, is it mandatory to use those "$", "%", and "#" signs in the code. I suppose i should study a lot to learn the meaning of those binary symbols. Thanks a lot sir.
 
% is short form for Integer.
$ means String.

It's from "classic" VB days and still there for backward compatibility (Identifier Type Characters). Some like to use them since it's faster to type and easy to remember.

But for legibility, unless you are used to it, I'd go with Literal Type Characters.

You may find link useful.
https://msdn.microsoft.com/en-us/library/s9cz43ek(v=VS.100).aspx

# in front of F, is processor command. See link.
http://chandoo.org/forum/threads/need-help-copy-excel-data-and-paste-to-notepad.28125/#post-168521
 
Back
Top