0

May I please have your feedback? It seems there are outdated answers to this question with language no longer applicable as it's throwing the runtime error 424 around -> Set objResultDiv = html.getElementById("rso")

The below code snippet is pasted from: How to get the first search result link of a google search using VBA?

Intended result: I'm trying to please search Google based on the contents of "Column A" in Excel, such that "Column B" will populate the url of the top Google search result.

Does anyone know the updated classes and requisite VBA references which will enable this here in 2023?

Sub Gethits()
    Dim url As String, lastRow As Long
    Dim XMLHTTP As Object, html As Object, objResultDiv As Object, objH3 As Object, link As Object
    Dim start_time As Date
    Dim end_time As Date
    Dim var As String
    Dim var1 As Object

    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 = 2 To lastRow

        url = "https://www.google.com/search?q=" & Cells(i, 1) & "&rnd=" & WorksheetFunction.RandBetween(1, 10000)

        Set XMLHTTP = CreateObject("MSXML2.serverXMLHTTP")
        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
        Set objResultDiv = html.getelementbyid("rso")
        Set var1 = html.getelementbyid("resultStats")
        Cells(i, 2).Value = var1.innerText

        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)
End Sub
Scott Craner
  • 148,073
  • 10
  • 49
  • 81
Justin
  • 1
  • The issue isn't with the VBA itself, the code is fine. Google is what has changed. Not certain, but their HTML/CSS looks like its designed to make scraping harder (as I think its against their TOS) – NickSlash Feb 15 '23 at 23:23
  • Thanks @NickSlash... had my fingers crossed... – Justin Feb 17 '23 at 00:30

0 Answers0