我尝试使用许多子/功能来解决我的问题。我正在寻找一个解决方案,如何从HTML-> URL中提取表格 - 当该页面上的表没有提到名称或id的标签时,通过它的类标签。
来源html:www.nbp.pl/home.aspx?f = / kursy / kursya.html https://d3higte790sj35.cloudfront.net/images/mg/xq/f16c888645b8ce4212b641de2441c7aa.jpeg
VBA spliting results from html imported table into excel
Excel VBA: get content from online HTML table
其他人......
答案 0 :(得分:0)
这应该适合你:
Sub Tester()
Dim IE As Object
Dim tbls, tbl, trs, tr, tds, td, r, c
Set IE = CreateObject("internetexplorer.application")
IE.Navigate "www.nbp.pl/home.aspx?f=/kursy/kursya.html"
Do While IE.Busy = True Or IE.readyState <> 4: DoEvents: Loop
Set tbls = IE.Document.getElementsByTagName("table")
Set tbl = IE.Document.getElementsByTagName("table")(31)
Set trs = tbl.getElementsByTagName("tr")
For r = 0 To trs.Length - 1
Set tds = trs(r).getElementsByTagName("td")
'if no <td> then look for <th>
If tds.Length = 0 Then Set tds = trs(r).getElementsByTagName("th")
For c = 0 To tds.Length - 1
ActiveSheet.Range("A2").Offset(r, c).Value = tds(c).innerText
Next c
Next r
End Sub
此代码会根据您在图片中显示的链接为您提供Tabela nr 103/A/NBP/2016 z dnia 2016-05-31
表格。表格将显示在Cell A2
。
这与@TimWilliams回答您提到的问题here的代码相同。
答案 1 :(得分:0)
我在你的帖子下谈论的代码和新页面。
`Sub Kraken()
Dim IE As Object
Dim tbls, tbl, trs, tr, tds, td, r, c
Set IE = CreateObject("internetexplorer.application")
IE.Navigate "https://www.kraken.com/charts"
Application.Wait Now + TimeSerial(0, 0, 1)
Set tbls = IE.Document.getElementsByTagName("table")
Set tbl = IE.Document.getElementsByTagName("table")(20)
Set trs = tbl.getElementsByTagName("tr")
For r = 0 To trs.Length - 1
Set tds = trs(r).getElementsByTagName("td")
'if no <td> then look for <th>
If tds.Length = 0 Then Set tds = trs(r).getElementsByTagName("th")
For c = 0 To tds.Length - 1
ActiveSheet.Range("J1").Offset(r, c).Value = tds(c).innerText
Next c
Next r
End Sub “DataTables_Table_1`