使用Excel中的VBA到IE中的Google搜索并返回第一个结果的超链接

时间:2013-07-05 19:25:25

标签: excel vba msxml

我一直在尝试使用IE自动化谷歌搜索Excel中的一串文本。我想在excel中的另一个单元格中返回第一个结果的网站的超链接。这可能吗?我有一个包含60,000条记录的列表,我需要谷歌搜索并在第一个结果中返回网站的超链接。你会推荐另一种解决方法吗?我提前感谢你的帮助。

2 个答案:

答案 0 :(得分:19)

作为其60,000条记录,我建议使用xmlHTTP对象而不是使用IE HTTP请求更容易,更快

Download the sample file

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

        url = "https://www.google.co.in/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 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_time = Time
    Debug.Print "end_time:" & end_time

    Debug.Print "done" & "Time taken : " & DateDiff("n", start_time, end_time) & " :minutes"
    MsgBox "done" & "Time taken : " & DateDiff("n", start_time, end_time)
End Sub

使用CSS3选择器

 Sub XMLHTTP1()

        Dim url As String, i As Long, lastRow As Long
        Dim XMLHTTP As Object, html As New HTMLDocument, objResultDiv As HTMLAnchorElement


        lastRow = Range("A" & Rows.Count).End(xlUp).Row
        For i = 2 To lastRow

            url = "https://www.google.co.in/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 = New HTMLDocument
            html.body.innerHTML = XMLHTTP.ResponseText
            Set objResultDiv = html.querySelector("div#rso h3.r a")

            Cells(i, 2) = objResultDiv.innerText
            Cells(i, 3) = objResultDiv.href

            DoEvents
        Next

    End Sub

<强>输出

enter image description here

HTH
桑托什

答案 1 :(得分:0)

链接似乎始终在H3标签内。通常情况下,您可能会使用以下内容进行检查,直到页面加载完毕为止:

Private Declare Sub Sleep Lib "kernel32" (ByVal nMilliseconds As Long)

Sub UseIE()
    Dim ie As Object
    Dim thePage As Object
    Dim strTextOfPage As String

    Set ie = CreateObject("InternetExplorer.Application")
    'ie.FullScreen = True
    With ie
        '.Visible = True
        .Navigate "http://www.bbc.co.uk"
        While Not .ReadyState = READYSTATE_COMPLETE '4
            Sleep 500      'wait 1/2 sec before trying again
        Wend
    End With

    Set thePage = ie.Document
    'more code here
End Sub

但是,我会反复尝试使用getElementsByTagName("H3")引用第一个H3中的A元素,获取第一个元素,然后在其中查找A-link及其href属性。

在JavaScript中,引用不存在的元素的尝试将返回undefined,但是从VBA开始,它可能需要错误处理代码。

获得 href 后,我会停止导航(不确定命令,可能是ie.Stop)或立即导航到下一页。

然而,第一个链接通常是赞助商链接,返回的href有点乱码。这些赞助商链接的文字似乎包含em个标签。我可能会使用这些信息来丢弃这些链接并进一步向下查看。

我不知道是否有更好的方法来做到这一点。