2014-10-28 60 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