如果使用电话号码,则通过 VBA 进行 Google 搜索没有结果 [英] Google search via VBA no results if use a phone number

查看:23
本文介绍了如果使用电话号码,则通过 VBA 进行 Google 搜索没有结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用我在网上找到的以下代码,搜索电话号码时不会返回结果,文本很好,带回网络链接和标题

Using following code I found on web it does not return results when searching for phone numbers, with text its fine, brings back weblink and title

我注意到,当搜索号码时,link.className 中没有 className "r",我将如何修复以使用电话号码

I have noticed that when search for number there is no className "r" in link.className, how would I fix to use with phone numbers

Sub XMLHTTP()

Dim url As String, lastRow As Long, i As Long
Dim XMLHTTP As Object, html As Object, objResultDiv As Object, objH3 As Object, link As Object

lastRow = Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To lastRow

  url = "https://www.google.co.uk/search?q=03701116565" & "&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 objH3 = objResultDiv.getelementsbytagname("h3")

    For Each link In objH3
        If link.className = "r" Then
            Cells(i, 2) = link.innerText
            Cells(i, 3) = link.getelementsbytagname("a")(0).href
            DoEvents
        End If
    Next
Next

End Sub

推荐答案

有一个类名 r.请注意以下事项:

There is a class name r. Observe the following:

Option Explicit
Public Sub GetLinks()
    Dim html As HTMLDocument, links As Object, i As Long, counter As Long
    Set html = New HTMLDocument
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://www.google.co.uk/search?q=03701116565", False
        .send
        html.body.innerHTML = StrConv(.responseBody, vbUnicode)
    End With

    With html
        Set links = .querySelectorAll(".r > [href] , .r h3")
    End With
    For i = 0 To links.Length - 1 Step 2
        counter = counter + 1
        ActiveSheet.Cells(counter, 1) = links.item(i)
        ActiveSheet.Cells(counter, 2) = links.item(i + 1).innerText
    Next
End Sub

实际的 href 与子 a 标记相关联,该标记位于您按类定位的 h3 标头标记元素之前.ra 标签的父类.

The actual href is associated with a child a tag which precedes the h3 header tag element which you are targeting by class. The r is the class of the parent of the a tag.

如果您想使用后期绑定以及与您类似的方法,您可以使用效率较低的以下方法.请注意,父 div 元素已被选中,因此合格类可以访问 a 标记和 h3.

If you want to use late bound, and a similar approach to yours, you can use the less efficient following method. Note that the parent div elements are selected so access to the a tag and h3 are possible for qualifying classes.

Option Explicit
Public Sub GetLinks()
    Dim html As Object, i As Long
    Dim objResultDiv As Object, objH3 As Object, link As Object

    Set html = CreateObject("htmlfile")
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://www.google.co.uk/search?q=03701116565", False
        .send
        html.body.innerHTML = .responseText
    End With

    Set objResultDiv = html.getElementById("rso")
    Set objH3 = objResultDiv.getElementsByTagName("div")
    For Each link In objH3
        If link.className = "r" Then
            i = i + 1
            On Error Resume Next
            ActiveSheet.Cells(i, 2) = link.getElementsByTagName("a")(0).href
            ActiveSheet.Cells(i, 3) = link.getElementsByTagName("h3")(0).innerText
            On Error GoTo 0
        End If
    Next
End Sub

这篇关于如果使用电话号码,则通过 VBA 进行 Google 搜索没有结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆