• 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.

Grab first link in google's search

YasserKhalil

Well-Known Member
Hello everyone
I have this code that I used before and it was working well but now gives me incorrect results ..
Can you point me how to fix it
The main thread is here
https://www.excelforum.com/excel-programming-vba-macros/

Code:
Public Sub Test()
    Dim IE As InternetExplorer
    Dim HTMLdoc As HTMLDocument
    Dim imgElements As IHTMLElementCollection
    Dim imgElement As HTMLImg
    Dim aElement As HTMLAnchorElement
    Dim N As Integer, I As Integer
    Dim Url As String, Url2 As String
    Dim LastRow As Long
    Dim m, sImageSearchString
  
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
  
    For I = 1 To LastRow
        Url = "http://www.google.co.in/search?q=" & Cells(I, 1) & "&source=lnms&tbm=isch&sa=X&rnd=1"
        Set IE = New InternetExplorer
      
        With IE
            .Visible = False
            .Navigate Url 'sWebSiteURL
          
            Do Until .readyState = 4: DoEvents: Loop
                'Do Until IE.document.readyState = "Complete": DoEvents: Loop
              
                Set HTMLdoc = .document
              
                Set imgElements = HTMLdoc.getElementsByTagName("IMG")
              
                N = 1
                For Each imgElement In imgElements
                    If InStr(imgElement.src, sImageSearchString) Then
                        If imgElement.ParentNode.nodeName = "A" Then
                            Set aElement = imgElement.ParentNode
                          
                            Url2 = imgElement.src
                            N = N + 1
                        End If
                    End If
                Next
              
                Cells(I, 2) = Url2
              
                IE.Quit
                Set IE = Nothing
            End With
        Next
End Sub
 
Last edited:
Hi !

Please correct link directly to the main thread, thanks !

When the webpage source code is modified,
the old VBA procedure may not work anymore …
 
Hi Yaasser! Is it necessary for you to stick to IE? If or if not, how about going for xmlhttp?
 
Thanks a lot for reply Shahin
In fact I have no great experience in dealing with scraping although I saw a lot of codes but I can't get it properly
So I accept anyway and I am not stick to specific approach ..
Regards
 
Back
Top