2017-04-12 96 views
0

我正在開發一個宏,用於從Internet Explorer表中獲取數據並在Excel表格中加載。從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 

這是

<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> 

據我所知,在頁面的HTML代碼的一部分在NG標記是表的內容,但我不知道如何得到它,我的電子表格..

感謝您的幫助

回答

0

嗯,我沒有看到你發佈的實際URL的位置,所以我只能做一個通用的建議,但我實際上無法爲你測試腳本。

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 

感謝您的腳本喬爾!根據您得到的結果,您是否可以進行適當的更改以從您正在使用的表中獲取數據?

+0

嗨Ryguy..Thanks非常有趣,因爲有了這個宏,我得到頁面的所有數據。但是可以選擇特定的標籤或ID來定界列D的信息嗎? – hdan7

+0

我不確定你的意思。你能發表一個例子嗎?另外,要知道你可以用IF ... THEN語句測試itm.innertext的內容。 – ryguy72

相關問題