此问题已在此处有答案:
Set HTML_Content = CreateObject("htmlfile") doesn't work(1个答案)
2天前关闭。
HTML我正试图从一个网页www.example.com获取数据https://www.lbma.org.uk/prices-and-data/precious-metal-prices#/table,看起来很简单,所有的q和a的例子都在网上,但我像一个腌鱼一样拍打着翅膀,经过多次尝试和错误后,我只能猜测。有人能告诉我哪里出错了吗?
目标,,,我的wbook数据一直定期手动更新,所以现在的目标是下载说,前10行的黄金价格只,最好没有欧元数据刚刚日期,美元和英镑。头也不是必需的,只是数据。
以下是迄今为止的HTML和代码。遇到的错误为“需要对象”和“对象不支持、、、”等。
Sub Get_Prices()
Dim sWeb_URL As String
Dim oHTML_Content As Object, oTbl As Object, tr As Object, td As Object, oTBody As Object
Dim r As Long, c As Long, arr
With Sheets(20)
sWeb_URL = "https://www.lbma.org.uk/prices-and-data/precious-metal-prices#/table"
Set oHTML_Content = CreateObject("htmlfile")
''get entire webpage content into HTMLFile Object
With CreateObject("msxml2.xmlhttp")
.Open "GET", sWeb_URL, False
.send
oHTML_Content.body.innerHTML = .responseText
End With
'Set oTbl = oHTML_Content.getElementsByTagName("-index1")
'Set oTbl = oHTML_Content.getElementById("-index1")
'Set oTbl = oHTML_Content.getElementsByClassName("pepper-responsive-table")
'Set oTbl = oHTML_Content.getElementsByClassName("pepper-responsive-table").getElementsByTagName("tbody")(0).getElementsByTagName("tr")(0)
'Set oTbl = oHTML_Content.getElementsByClassName("pepper-responsive-table")(0).getElementsByTagName("tr")(2)
Set oTbl = oHTML_Content.getElementsByTagName("tbody")
For Each tr In oTbl
c = 1
For Each td In tr.Cells
.Cells(r, c) = td.innerText
c = c + 1
Next td
r = r + 1
Next tr
End With
End Sub
1条答案
按热度按时间7gcisfzg1#
在阅读@Zwenn的评论之后,我编写了以下代码,并将值带到工作表中。