2014-10-28 118 views

回答

1

较小修改原来的代码中,我们可以捕捉resultStats div的innerText属性。 Excel表格中的第一列包含要搜索的字符串。第二栏将填写“约1,660个结果(0.17秒)”等结果。如果您只需要1660这样的数字,可以使用简单的字符串函数(instr,mid等)来解析并获得所需的结果。

Sub XMLHTTP_Count()  
    Dim url As String, lastRow As Long 
    Dim XMLHTTP As Object, html 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.XMLHTTP") 
     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 

    If html.getElementById("resultStats") Is Nothing Then 
     str_text = "0 Results" 
    Else 
     str_text = html.getElementById("resultStats").innerText 
    End If 
     Cells(i, 2) = str_text 
     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