从Internet Explorer获取数据以获得优秀

时间:2017-04-12 15:02:23

标签: html angularjs excel vba excel-vba

我正在开发一个宏来从Internet Explorer表中获取数据并加载到Excel中的工作表中。

使用此代码,我可以获得表格的标题,但我无法获得内容

    Sub extraerDatos()

    Dim URL As String
    Dim IE As InternetExplorer
    Dim HTMLdoc As HTMLDocument
    Dim TDelements As IHTMLElementCollection
    Dim TDelement As HTMLTableCell
    Dim oElement As Object
    Dim r As Long


    URL = "URL"

    Set IE = New InternetExplorer

    With IE
        .Navigate URL
        .Visible = True

        'Cargar pagina
        While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend

        Set HTMLdoc = .Document
    End With
  Application.Wait (Now + TimeValue("0:00:10"))

Set dados = HTMLdoc.getElementsByClassName("tableContainer")
 (0).getElementsByTagName("span")
    I = 0
 For Each oElement In dados
    Sheets("Hoja1").Range("A" & I + 1) = dados(I).innerText
    I = I + 1
 Next oElement
End Sub

这是页面HTML代码的一部分

<div class="tableContainer"  >
        <div class="tlnTable tlnw12" id="tlnAngTableTransfers" style="display:none" >
            <span style="margin:0; padding-bottom: 10px;" class="field-validation-error ng-binding" ng-show="model.bTransferRequired">Select at least one transfer</span>
            <div class="tlnHeader tlnw12"> <!-- HEADER -->
                <div class="tlnw3">
                    <div class="tlnw1 tlntd">
                        <input type="checkbox" ng-model="model.grid.selected" ng-change="model.grid.selectAll()"/> 
                    </div>
                    <div class="tlnw6 tlntd">
                        <a href="#" class="tlnSortLink tlnw12" ng-click="model.grid.sortableColumns.columns.transferId.changeSort()">
                            <span class="tlnw10">Transfer ID / PO-SO Number </span>
                            <span class="tlnSortIco tlnw2" ng-show="model.grid.sortableColumns.lastSortedColumn.id === model.grid.sortableColumns.columns.transferId.id" >
                                <span class="tlnSortAsc tlnw12 " ng-class="{act: model.grid.sortableColumns.lastSortedColumn.modifier==='asc', inact: model.grid.sortableColumns.lastSortedColumn.modifier==='desc'}" ></span>
                                <span class="tlnSortDesc inact tlnw12" ng-class="{act: model.grid.sortableColumns.lastSortedColumn.modifier==='desc', inact: model.grid.sortableColumns.lastSortedColumn.modifier==='asc'}"></span>
                            </span>
                        </a>
                    </div>

我明白NG标签是表的内容,但我不知道如何获取它,对于我的电子表格..

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

好吧,我不知道你在哪里发布了实际的网址,所以我所能做的就是做一个通用推荐,但我实际上无法为你测试这个脚本。

Sub DumpData()

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True

URL = "http://finance.yahoo.com/q?s=sbux&ql=1"

'Wait for site to fully load
IE.Navigate2 URL
Do While IE.Busy = True
   DoEvents
Loop

RowCount = 1

With Sheets("Sheet1")
   .Cells.ClearContents
   RowCount = 1
   For Each itm In IE.document.all
      .Range("A" & RowCount) = itm.tagname
      .Range("B" & RowCount) = itm.ID
      .Range("C" & RowCount) = itm.classname
      .Range("D" & RowCount) = Left(itm.innertext, 1024)

      RowCount = RowCount + 1
   Next itm
End With
End Sub

感谢剧本Joel !!根据您获得的结果,您是否可以进行适当的更改以从您正在使用的表中获取数据?