使用VBA在网站上搜索

时间:2018-03-21 16:52:15

标签: excel vba excel-vba

我正试图围绕这个问题,因为在线查看这段代码虽然基本上是我能理解的,因为不了解VBA,至少应该把谷歌的搜索栏放在我放入的单词中。但它似乎不是我错过了什么或者我完全错了任何指针?

我知道它还没有点击输入,因为还没有添加,它会加载谷歌页面,但就是这样。

理想情况下,在进行搜索之后,将其展开以使用电子表格中的信息更新内部网站

如果有人知道任何有关VBA代码意义的好地方,请告知。

Sub test()
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String

On Error GoTo Err_Clear
MyURL = "google.com"
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.document
ie.document.getElementById("lst-ib").Value = "bbc"
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub

2 个答案:

答案 0 :(得分:2)

目前,您只能在搜索结果搜索结果之后使用以下任一项,其中searchTerm =“BBC”会连接到网址中。 XMLHTTP60将是您的Excel作为XML参考库的版本。

Option Explicit

Sub Getinfo2()

    Dim http As New XMLHTTP60
    Dim html As New HTMLDocument
    Dim searchTerm As String

    searchTerm = "BBC"

    With http
        .Open "GET", "https://www.google.co.uk/search?safe=strict&source=hp&ei=Ep2yWvPDN8visAfVwIagBg&q=" & searchTerm & "&oq=" & searchTerm & "&gs_l=psy-ab.3..35i39k1j0i131i67k1l3j0i131k1j0i131i67k1l3j0i67k1j0i131i67k1.1897.1897.0.3045.3.2.0.0.0.0.134.134.0j1.2.0....0...1.1.64.psy-ab..1.2.269.6...135.zC-Z7B8DrM4", False
        .send
        html.body.innerHTML = .responseText
    End With

    Dim posts As MSHTML.IHTMLElementCollection 'add stuff to a collection etc
    Dim post As MSHTML.IHTMLElement

    '......


End Sub

或使用IE

Public Sub ScrapeIE()

    Dim appIE As Object
    Dim ihtml As Object
    Dim searchTerm As String

    searchTerm = "BBC"
    Set appIE = CreateObject("internetexplorer.application")

    With appIE

        .Visible = True
        .navigate "https://www.google.co.uk/search?safe=strict&source=hp&ei=Ep2yWvPDN8visAfVwIagBg&q=" & searchTerm & "&oq=" & searchTerm & "&gs_l=psy-ab.3..35i39k1j0i131i67k1l3j0i131k1j0i131i67k1l3j0i67k1j0i131i67k1.1897.1897.0.3045.3.2.0.0.0.0.134.134.0j1.2.0....0...1.1.64.psy-ab..1.2.269.6...135.zC-Z7B8DrM4"

        While .Busy = True Or .readyState < 4: DoEvents: Wend

        Set ihtml = .document


       ' .Quit

    End With

    Set appIE = Nothing

End Sub

答案 1 :(得分:0)

你想做什么? Google会搜索并计算点击次数吗?尝试下面的脚本并给我反馈。

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

将您的搜索字词放在ColumnA中并运行脚本。

enter image description here