Error on getting the URL result of a Google search.

后端 未结 3 2003
轻奢々
轻奢々 2021-01-27 00:26

I am new to VBA, and I figured that trying to code is the best way to code. Anyway, I am trying to code a macro that will get first URL of a Google search result, but I\'m getti

3条回答
  •  一整个雨季
    2021-01-27 00:53

    In the zero result case, H3 is empty so modify your code like this to handle this case

      Set html = CreateObject("htmlfile")
      html.body.innerhtml = XMLHTTP.ResponseText
      Set objResultDiv = html.getelementbyid("rso")
    
      **numb_H3 = objResultDiv.getElementsByTagName("H3").Length**
      **If numb_H3 > 0 Then**
          Set objH3 = objResultDiv.getElementsByTagName("H3")(0)
          Set link = objH3.getElementsByTagName("a")(0)
    
          str_text = Replace(link.innerhtml, "", "")
          str_text = Replace(str_text, "", "")
    
          Cells(i, 2) = str_text
          Cells(i, 3) = link.href
      **Else**
      **End If**
      DoEvents
    

    Next

提交回复
热议问题