我在B栏中有一些文字,我正在尝试在C列中记录Google搜索结果的数量。搜索将在下面给我这个页面,我将需要从这里搜索结果的数量: Google search
我使用的代码是:
Sub HawkishSearch()
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("B" & 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, 2) & "&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, 3) = 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
此代码存在两个问题。
首先,我得到了整个结果"关于xxxx结果(x.xx秒)"。我只需要搜索结果的数量。我尝试使用:
Cells(i, 3) = Mid(str_text, 7, Len(str_text) - 30)
但如果有例如0结果会给我一个错误。
其次,最重要的是,我收到错误XMLHTTP.send
:
答案 0 :(得分:0)
改变
Set XMLHTTP = CreateObject("MSXML2.XMLHTTP")
到
Set XMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")