【问题标题】:Recording number of search results from Google记录来自 Google 的搜索结果数量
【发布时间】:2015-12-09 19:35:44
【问题描述】:

我在 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 收到错误:

【问题讨论】:

  • P.S.这是我收到错误的原因吗?在那种情况下,有人可以建议这是否可行吗? quora.com/…

标签: vba excel


【解决方案1】:

更改
Set XMLHTTP = CreateObject("MSXML2.XMLHTTP")


Set XMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 2018-09-21
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多