【问题标题】:accessing html elements of ASX website dividend table using excel vba使用 excel vba 访问 ASX 网站股息表的 html 元素
【发布时间】:2013-12-13 09:51:17
【问题描述】:

我正在尝试从 ASX 下载股息数据!

我需要知道如何循环遍历表中的每一行以及该表中的每个 td(数据元素)以将内部文本打印到 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

【问题讨论】:

  • 相信你的网站充满了垃圾邮件,它唯一的赞助链接。你想拉什么?
  • 我只是注意到当您使用网络查询时它只显示两行,但如果您访问该网站:asx.com.au/asx/markets/… 它显示所有四行(多年的红利)
  • 我还没有托管那个网站。对此感到抱歉。 :(
  • 您可以使用返回 JSON 的 ASX API,例如 asx.com.au/asx/1/company/ONT/dividends/history?years=10

标签: html excel vba stocks stockquotes


【解决方案1】:

试试这个

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

【讨论】:

  • @user3098818 我删除了不需要的变量。
  • 现在正在尝试。仅打印表格最后一行中的数据,请参见链接。仅以绿色打印底线。 drive.google.com/file/d/0B9QWdVxiXWaRbnBpTGp1S2ZFU0U/…
  • @user3098818 第一行是带有 TH(Table Header) 标记的标题行/第二行以后将是 TD
  • @user3098818 如果要打印标题,请更改此Set tdCollection = trObj.getElementsByTagName("th")
  • 自动化也可以在 FF 中完成。好的,我可以用它访问网站,但不知道如何循环遍历元素......也许我将不得不使用 IE。在这种情况下,鉴于所有浏览器现在只显示一年的红利,你的代码运行得非常好。感谢您的耐心等待。
猜你喜欢
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-07
  • 1970-01-01
相关资源
最近更新 更多