So I am trying to get the address of a company when you search for [Company name] + "Address" on google. I can get the "About 20,500 results (0.49 seconds)" no issue because the div id is always resultStats however, for the address its a HTML Class that is called: Z0LcW. Not sure if it changes.
I am trying to confirm that the trading addresses provided by a potential supplier are valid. Its for 4k addresses and i dont want to have to manually verify it.
The idea came from: Using VBA in Excel to Google Search a keyword and return indexed pages on google
I have looked at these guys who were very helpful conceptually: Using getElementsByClassName in Excel VBA
Dim XMLHTTP As Object, html, ObjResultdiv As Object
Dim start_time As Date
Dim end_time As Date
lastRow = Range("A" & Rows.Count).End(xlUp).Row
Dim cookie As String
Dim result_cookie As String
start_time = Time
Debug.Print "start_time:" & start_time
For i = 1 To lastRow
url = "https://www.google.co.uk/search?q=" & Cells(i, 1) & "&rnd=" & WorksheetFunction.RandBetween(1, 10000)
Set XMLHTTP = CreateObject("MSXML2.XMLHTTP")
XMLHTTP.Open "GET", url, False
XMLHTTP.setRequestHeader "Content-Type", "text/xml"
XMLHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"
XMLHTTP.send
Set html = CreateObject("htmlfile")
html.body.innerHTML = XMLHTTP.responseText
'Debug.Print XMLHTTP.ResponseText
If html.getElementsBytagname("Z0LcW") Is Nothing Then
str_text = "0 Results"
Else
str_text = html.getElementsBytagname("Z0LcW")
End If
Cells(i, 2) = str_text
DoEvents
Next
end_time = Time
Debug.Print "end_time:" & end_time
Debug.Print "done" & "Time taken : " & DateDiff("n", start_time, end_time)
MsgBox "done" & "Time taken : " & DateDiff("n", start_time, end_time)
The answer should be: 31B, Avenue One, Station Ln, Witney OX28 4XZ