html - VBA:从 HTMLTable 中抓取确切的元素
问题描述
请你能帮我理解如何a
在Table
类名中找到标签bptable
吗?
我收到Object does not support this method
了,但我不知道如何解决这个问题。
Sub ListVideosOnPage(VidCatName As String, VidCatURL As String)
Dim XMLReq As New MSXML2.XMLHTTP60
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim VidRow As MSHTML.IHTMLElement
Dim VidInnerRow As MSHTML.IHTMLElement
Dim VidRows As MSHTML.IHTMLElementCollection
Dim VidInnerRows As MSHTML.IHTMLElementCollection
Dim VidInnerCatID As Integer
XMLReq.Open "GET", VidCatURL, False
XMLReq.send
If XMLReq.Status <> 200 Then
MsgBox "Problem" & vbNewLine & XMLReq.Status & " - " & XMLReq.statusText
Exit Sub
End If
HTMLDoc.body.innerHTML = XMLReq.responseText
Set XMLReq = Nothing
Set VidRows = HTMLDoc.getElementsByClassName("bptable")
Set VidInnerRows = ***VidRows***.getElementsByTagName("a")
With VidRows
For VidInnerCatID = 2 To VidInnerRows.Length
Set VidInnerRow = VidInnerRows(VidInnerCatID)
'Debug.Print
Next VidInnerCatID
End With
End Sub
解决方案
除了使用.getElementsByTagName("a")
元素集合之外,您还可以使用VidRows(0)
或尝试for loop
获取单个元素以应用于.getElementsByTagName("a")
它们。我会选择一个for loop
来达到这个目的。以下是获取内容的一种方式。
Sub ListVideosOnPage(VidCatName As String, VidCatURL As String)
Dim XMLReq As New XMLHTTP60
Dim HTMLDoc As New HTMLDocument
Dim VidInnerRows As Object
Dim R As Long
XMLReq.Open "GET", VidCatURL, False
XMLReq.send
If XMLReq.Status <> 200 Then
MsgBox "Problem" & vbNewLine & XMLReq.Status & " - " & XMLReq.statusText
Exit Sub
End If
HTMLDoc.body.innerHTML = XMLReq.responseText
Set XMLReq = Nothing
For Each VidInnerRows In HTMLDoc.getElementsByClassName("bptable")
With VidInnerRows.getElementsByTagName("a")
If .Length Then R = R + 1: Cells(R, 1) = .Item(0).innerText
End With
Next VidInnerRows
End Sub
推荐阅读
- r - 使图形缩小
- xml - 在 oracle 中使用 select 提取 xmltype 时出现无效字符
- azure-cosmosdb - CosmosDb 在带有数组的“array_contains”的查询中抛出异常?
- postgresql - PostgreSQL 的数据文件中可以容纳多少行?
- javascript - HTML 和 Javascript:如何将包含用户输入的段落元素附加到 div 元素中?
- javascript - 通过javascript访问json中的元素
- python - 安装后如何修复 psycop2 导入不起作用
- javascript - javascript中的数组异步调用(Promise.all)
- flutter - 如果值为负,则颤动会更改字体颜色
- reactjs - 从表单输入生成字母数字 URL