使用excel vba自动进行Google查询:任何人都可以使用此功能吗?

时间:2017-08-02 19:39:23

标签: excel-vba vba excel

社区的新手,并寻找一种方法来使用excel vba自动执行Google搜索,然后解析标题下方的“大约49,600个结果(0.78秒)”信息。我最近发现这个教程似乎(或声称)完成了这一点。 - > http://yodalearning.com/tutorials/perform-google-search-internet-explorer-using-excel-vba-excel-vba/

有人能让这个工作吗?

当我尝试运行该模块时,它会打开谷歌,但随后负载圈无效。如果有人知道如何让这个工作,任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

将以下内容放入A2:A4

girafe
rhino
starbucks

运行以下脚本。

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

的Mozilla: enter image description here