【问题标题】:Navigate a website with Excell Vba使用 Excel Vba 导航网站
【发布时间】:2011-02-04 01:29:25
【问题描述】:

我可以从已知的 ulr 中提取数据,但我可以使用 excel 浏览网站。

例如,可以用 excel 进行谷歌搜索并将结果放在电子表格中。或浏览框架内网站。

此代码从网站中提取数据。

With ActiveSheet.QueryTables.Add(Connection:= _
    PUT_URL_HERE, _
    Destination:=Range("A1"))
    .Name = "I need serious help"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = False
    .RefreshPeriod = 0
    .WebSelectionType = xlAllTables
    .WebFormatting = xlWebFormattingNone
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
End With

【问题讨论】:

  • 创建一个宏来跟踪我进行网络查询并不能完全破解它。
  • 考虑导航基于 HTML 的网站涉及的内容:您需要发送带有适当数据的请求,然后读取返回的文档。 Excel 可以通过引用适当的库来通过 VBA 完成这两件事
  • 很高兴知道这是可能的。你知道任何好的来源,或者这样做的例子吗

标签: excel vba


【解决方案1】:

嘿,所以我想出了这个,并认为我会发布以防万一有人遇到类似问题。

Sub googlesearch()
    Set objIE = CreateObject("InternetExplorer.Application")
    WebSite = "www.google.com"
    With objIE
        .Visible = True
        .navigate WebSite
        Do While .Busy Or .readyState <> 4
            DoEvents
        Loop

        Set Element = .document.getElementsByName("q")
        Element.Item(0).Value = "Hello world"
        .document.forms(0).submit
        '.quit
        End With

End Sub

所以要成功做到这一点,你需要知道元素名称(你可以通过查看源代码或安装一个可以帮助你喜欢 firebug 的插件来做到这一点)你也可以使用 .getElementsByID("someIDhere")。

【讨论】:

    【解决方案2】:

    我知道这有点晚了,但这里有一个解决方案。你需要做界面来传递搜索词,如果需要,验证/编码它等。它需要你的电脑上的 Internet Explorer。它会为您提供原始 HTML,您必须根据需要对其进行解析。

    Function GoogleSearch(strSearchTerm As String) As String
       Dim ie As Object
       Dim sHTML As String
    
       On Error GoTo ZERR
       Set ie = CreateObject("InternetExplorer.Application")
    
       With ie
           .Visible = False
           .navigate ("http://www.google.com/search?q=" & strSearchTerm)
           Do Until .readystate = 4
               DoEvents
           Loop
           sHTML = .document.Body.innerHTML
       End With
       Set ie = Nothing
    GoogleSearch = sHTML    
    ZERR:
       Set ie = Nothing
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多