XML中的ExcelHTTP数据提取 - 无法提取正确的数据

时间:2016-04-01 18:09:09

标签: vba excel-vba xmlhttprequest getelementsbyclassname data-extraction

我的VBA知识有限,我正在努力学习,但我仍然坚持使用下面的代码。我的问题是我正在尝试在包含在表类“Standard_tabelle”中的此网页上提取结果表。

Sub seasonres()

Sheets("Vs").Select
Range("A1").Select
Dim url As String, links_count As Integer
Dim i As Integer, j As Integer, row As Integer
Dim XMLHTTP As Object, html As Object
Dim tr_coll As Object, tr As Object
Dim td_coll As Object, td As Object

  url = "http://www.worldfootball.net/teams/manchester-united/2011/3/"

    Set XMLHTTP = CreateObject("MSXML2.XMLHTTP")
    XMLHTTP.Open "GET", url, False
    XMLHTTP.send

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

    Set tbl = html.getelementsbytagname("Standard_tabelle")

    Set tr_coll = tbl(0).getelementsbytagname("TR")

    For Each tr In tr_coll
        j = 1
        Set td_col = tr.getelementsbytagname("TD")

        For Each td In td_col
            Cells(row + 1, j).Value = td.innerText
            j = j + 1
        Next
        row = row + 1
    Next
End Sub

但是下面的代码一直给我一个运行时错误,当我调试它时指向:

Set tr_coll = tbl(0).getelementsbytagname("TR")

是否有人可以帮助我更正此代码以提取表格或向我显示另一个有助于此的代码?

非常感谢提前。 沙希德

1 个答案:

答案 0 :(得分:0)

我认为htmlfile可能不支持getElementsByclassname ...

尝试这种解决方法

Sub seasonres()

    Dim url As String, links_count As Integer
    Dim i As Integer, j As Integer, row As Integer
    Dim XMLHTTP As Object, html As Object
    Dim tr_coll As Object, tr As Object
    Dim td_coll As Object, td As Object, tbl, td_col, objT

    url = "http://www.worldfootball.net/teams/manchester-united/2011/3/"

    Set XMLHTTP = CreateObject("MSXML2.XMLHTTP")
    XMLHTTP.Open "GET", url, False
    XMLHTTP.send

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

    Set tbl = html.body.getelementsbytagname("table")

    For Each t In tbl
        If t.classname = "standard_tabelle" Then
            For Each tr In t.getelementsbytagname("TR")
                j = 1
                For Each td In tr.getelementsbytagname("TD")
                    Cells(row + 1, j).Value = td.innerText
                    j = j + 1
                Next
                row = row + 1
            Next
            row = row + 3
        End If
    Next


End Sub
相关问题
最新问题