0

我需要使用以下链接(警告,乌克兰语)获得的信息不断更新 excel 文件: 链接到乌克兰财政部网站

有用的数据由 HTML 标签包裹<tbody></tbody>

我需要从表中检索信息的类似代码

Set htm = CreateObject("htmlFile")' #it doesn't work on mac os machine, but perfectly performs on windows
    With CreateObject("msxml2.xmlhttp")
        .Open "GET", <site_url_goes_here>, False
        .send
        htm.body.innerhtml = .responsetext
    End With

    With htm.getelementbyid("item")' <<<<<---what should I write here in order to parse data from the web-site table?
        Sheet2.Cells(Row, 4).Value = p
        For x = 1 To .Rows.Length - 1
            For y = 0 To .Rows(x).Cells.Length - 1
                Sheet2.Cells(Row, y + 1).Value = .Rows(x).Cells(y).innertext
            Next y
            Row = Row + 1
        Next x
    End With`
4

1 回答 1

0

下面的代码将每 60 秒从http://www.minfin.gov.ua获取更新数据。

Sub getData()

    Application.OnTime Now + TimeSerial(0, 0, 60), "finance_data"

End Sub

Private Sub finance_data()
    Dim url As String, lastRow As Long
    Dim XMLHTTP As Object, html As Object
    Dim tbl As Object, obj_tbl As Object
    Dim TR As Object, TD As Object
    Dim row As Long, col As Long

    lastRow = Range("A" & Rows.Count).End(xlUp).row

    url = "http://www.minfin.gov.ua/control/uk/publish/article?art_id=384069&cat_id=234036" & "&r=" & WorksheetFunction.RandBetween(1, 10000)

    Set XMLHTTP = CreateObject("MSXML2.XMLHTTP")
    XMLHTTP.Open "GET", url, False
    XMLHTTP.setRequestHeader "Content-Type", "text/xml"
    XMLHTTP.send

    Set html = CreateObject("htmlfile")
    html.body.innerHTML = XMLHTTP.ResponseText
    Set obj_tbl = html.getelementsbytagname("table")

    row = 1
    col = 1

    For Each tbl In obj_tbl
        If tbl.classname = "MsoNormalTable" Then
            Set TR = tbl.getelementsbytagname("TR")

            For Each obj_row In TR
                For Each TD In obj_row.getelementsbytagname("TD")
                    Cells(row, col) = TD.innerText
                    col = col + 1
                Next
                col = 1    ' reseting the value
                row = row + 1
            Next
        End If
    Next

    getData
End Sub
于 2013-11-15T03:01:21.520 回答