excel 中的 XMLHTTP 数据提取 - 无法提取正确的数据
XMLHTTP Data extract in excel - unable to extract the correct data
我的 VBA 知识有限,我正在努力学习,但我坚持使用下面的代码。我的问题是我正在尝试提取此网页上的结果 table table class "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
但是下面的代码一直给我一个 运行-time 错误,当我调试它时它指向:
Set tr_coll = tbl(0).getelementsbytagname("TR")
是否有人可以帮助我更正此代码以提取 table 或向我展示另一个有助于解决此问题的代码?
非常感谢。
沙希德
我想 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
我的 VBA 知识有限,我正在努力学习,但我坚持使用下面的代码。我的问题是我正在尝试提取此网页上的结果 table table class "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
但是下面的代码一直给我一个 运行-time 错误,当我调试它时它指向:
Set tr_coll = tbl(0).getelementsbytagname("TR")
是否有人可以帮助我更正此代码以提取 table 或向我展示另一个有助于解决此问题的代码?
非常感谢。 沙希德
我想 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