2013-12-13 51 views
0

我想從ASX下載divident數據!使用excel vba訪問ASX網站股息表的html元素

我需要知道如何遍歷表中的每一行以及該表中的每個td(數據元素),以便將excel表格中的內聯網格打印到excel工作表中。

這裏是我的代碼....使用以此爲指導

VBA excel For each row in table match cell in spreadsheet with cell in webpage table

Sub WebTable() 

Dim ie As New InternetExplorer 
Set ie = Nothing 
ie.Visible = False 
Dim url As String 
Dim doc As HTMLDocument 
Dim items As Variant 
Dim tr As String 
Dim tbody As String 
Dim td As String 
Dim r As Double 
Dim i As Double 

Dim tbObj As Variant 
Dim trObj As Variant 
Dim tdObj As Variant 
Dim tdItem As Variant 
Dim element 

    tbody = "tbody" 
    tr = "tr" 
    td = "td" 
    url = "http://www.asx.com.au/asx/markets/dividends.do?by=asxCodes&asxCodes=ont&view=latest" 

    ie.navigate url 
    Debug.Print url 

    'loop until complete so program doesn't freeze 
    Do 
    DoEvents 
    Loop Until ie.readyState = READYSTATE_COMPLETE 

'get everything in the dividends table using its id 
    Set tbObj = ie.document.getElementById("dividends") 'all table elements including headers 

    'get the four rows for the ONT stock (headings and data elements 
    Set trObj = tbObj.getElementsByTagName(tr) 


    'get the eight data items for each row of the ONT stock e.g stock price date etc. 
    For Each trObj In tbObj 

'print each data element one by one for each of the four rows 
     Set tdObj = tbObj.items(0).getelementbytagname("td") 
     Set Count = tdObj.length 
     Debug.Print tdObj(0).innerText 
    Next trObj 

End Sub 
+0

桑托斯您weebsite充滿垃圾的其只有贊助鏈接。你想拉什麼? – yoshiserry

+0

我剛剛注意到它只顯示兩行,當您使用網絡查詢,但如果您轉到該網站: http://www.asx.com.au/asx/markets/dividends.do?by=asxCodes&asxCodes=ont&view=最新 其顯示全部四行(多年分紅) – yoshiserry

+0

我還沒有託管該網站。對不起。 :( – Santosh

回答

0

試試這個

Sub WebTable() 

    Dim ie As New InternetExplorer 
    Dim url As String 

    Dim trCollection As Object 
    Dim tdCollection As Object 

    Dim tdObj As Object 
    Dim trObj As Object 


    url = "http://www.asx.com.au/asx/markets/dividends.do?by=asxCodes&asxCodes=ont&view=latest" 

    ie.navigate url 
    Debug.Print url 


'loop until complete so program doesn't freeze 
    Do 
     DoEvents 
    Loop Until ie.readyState = READYSTATE_COMPLETE 

'get everything in the dividends table using its id 
    Set tbObj = ie.document.getElementById("dividends") 'all table elements including headers 

'get the four rows for the ONT stock (headings and data elements 
    Set trCollection = tbObj.getElementsByTagName("tr") 

Dim i As Integer 
i = 1 
'get the eight data items for each row of the ONT stock e.g stock price date etc. 
    For Each trObj In trCollection 

    If i = 1 Then 
'print each data element one by one for each of the four rows 
     Set tdCollection = trObj.getElementsByTagName("th") 
     Else 
      Set tdCollection = trObj.getElementsByTagName("td") 

     End If 

     i = i + 1 

     For Each tdObj In tdCollection 

      Debug.Print tdObj.innerText 
     Next 

    Next 

End Sub 

enter image description here

+0

@ user3098818我已經刪除了不需要的變量。 – Santosh

+0

現在試圖。只打印數據在表的最後一行看到鏈接。只打印綠色底線。 https://drive.google.com/文件/ d/0B9QWdVxiXWaRbnBpTGp1S2ZFU0U /編輯?usp =共享 – yoshiserry

+0

@ user3098818第一行是TH(表頭)標記/第二行向前的標題行,它將是TD – Santosh