我正在尝试将内部网页上的表格内容放入 Excel 工作表中,但我无法获取任何 HTML 元素。 我尝试通过 ID、类名和标记名获取所有元素,但无济于事。有人可以告诉我我做错了什么吗?
Sub Shuffle()
Application.DisplayAlerts = False
'On Error GoTo ErrHandler
Dim SheetName, FavNumber As String
'LTI
SheetName = "LTI"
FavNumber = "2282804"
'Call IE_Sledgehammer
Dim ie As InternetExplorerMedium, I As Long, strText As String
Dim doc As Object, hTable As Object, hBody As Object, hTR As Object, hTD As Object
Dim tb As Object, bb As Object, tr As Object
Dim y As Long, z As Long, wb As Excel.Workbook, ws As Excel.Worksheet
Set wb = Excel.ActiveWorkbook
'Set ws = wb.ActiveSheet
'LastRow = Sheets(SheetName).Cells(Sheets(SheetName).Rows.Count, "A").End(xlUp).Row
'If LastRow > 1 Then
' Sheets(SheetName).Range("A2:AA" & LastRow + 1).ClearContents
'End If
'Application.Wait (Now + TimeValue("0:00:02"))
Set ie = Nothing
Application.Wait (Now + TimeValue("0:00:02"))
Set ie = New InternetExplorerMedium
ie.Visible = True
ie.navigate "https://synergi.de-prod.dk/synergi/favourite/" & FavNumber
On Error Resume Next
Do While ie.Busy: DoEvents: Loop
Do While ie.readyState <> 4:
Set doc = ie.document
Dim td As Object
Set td = doc.getElementsByTagName("td")
Set Post6a = doc.getElementsByTagName("th")(0).outerText: MsgBox Post6a
Set Post6b = doc.getElementsByTagName("th")(0).innerText: MsgBox Post6b
Set Post6c = doc.getElementsByTagName("td"): MsgBox Post6c.Length
Dim th As Object
Set th = doc.getElementsByTagName("th")
Set Post8a = doc.getElementsByTagName("td")(0).outerText: MsgBox Post8a
Set Post8b = doc.getElementsByTagName("td")(0).innerText: MsgBox Post8b
Set Post8c = doc.getElementsByTagName("th"): MsgBox Post8c.Length
Dim span As Object
Set span = doc.getElementsByTagName("span")
Set Post7a = doc.getElementsByTagName("span")(0).outerText: MsgBox Post7a
Set Post7b = doc.getElementsByTagName("span")(0).innerText: MsgBox Post7b
Set Post7c = doc.getElementsByTagName("span"): MsgBox Post7c.Length
Dim div As Object
Set span = doc.getElementsByTagName("div")
Set Post9a = doc.getElementsByTagName("div")(0).outerText: MsgBox Post9a
Set Post9b = doc.getElementsByTagName("div")(0).innerText: MsgBox Post9b
Set Post9c = doc.getElementsByTagName("div"): MsgBox Post9c.Length
y = 1 'Column A in Excel
z = 2 'Row 1 in Excel
Set hTable = doc.getElementsByTagName("table")
For Each tb In hTable
Set hBody = tb.getElementsByTagName("tbody")
For Each bb In hBody
Set hTR = bb.getElementsByTagName("tr")
For Each tr In hTR
Set hTD = tr.getElementsByTagName("td")
y = 1 ' Resets back to column A
For Each td In hTD
wb.Sheets(SheetName).Cells(z, y).Value = td.innerText
y = y + 1
'MsgBox td.innerText
Next td
DoEvents
z = z + 1
Next tr
Exit For
Next bb
Exit For
Next tb
Application.Wait (Now + TimeValue("0:00:02"))
'IE.Quit
ie.Quit
End Sub
答案 0 :(得分:0)
如果需要将网站的表格内容提取到Excel,可以试试这两种方法:
使用 IE:数据位于需要协商的 iframe 内
使用 XMLHTTP 请求 - 更快,无需打开浏览器。它使用 iframe 文档 URL 的第一部分,即 iframe 导航到的位置。
更多细节可以参考下面的案例,其中包含一个简单的例子: Extract table from webpage using VBA