VBA / DOM-根据属性获取元素 [英] VBA/DOM - Get elements based on attribute
问题描述
Windows 7上的Excel2013。 XPath
/ Javascript
/ jQuery
超出范围。
Excel 2013 on Windows 7. XPath
/Javascript
/jQuery
is out of scope.
我试图遍历页面中选定的 div
元素,即具有特定数据级
属性的元素。
I am trying to iterate over select div
elements in a page, namely elements that have a specific data-level
attribute.
我当前的方法是与此类似,但我无法找到一种非手动方式来基于属性选择元素。我最接近的是这样的:
My current approach is similar to this, but I was unable to find a non-manual way to select elements based on attributes. The closest I came was something like:
With CreateObject("WINHTTP.WinHTTPRequest.5.1")
.Open "GET", url, False
.Send
pHTML.body.innerHTML = .ResponseText
End With
Set eCollection = pHTML.getElementsByClassName("chapter").getElementsByTagName("div")
For i = 0 To eCollection.Length
If eCollection(i).getAttribute("data-level") >= 0 Then ' Throw cake
Next i
虽然我确信它是可行的(如果不确定),但这种解决方案似乎不可行-optimize(如果仅对于循环将要结束的大小而言)是当我开始在这些元素中寻找特定元素和元素序列时。
This solution, while I am sure it is viable (if unelegant), seems sub-optimal if only for how big the loop is going to end up being when I start looking for specific elements and sequences of elements within these elements.
因此,我正在寻找一种执行以下操作的方法:
So I am looking for a way to do something like this:
For Each pElement In pHTML.getElementsByClassName("chapter").getElementsByTagName("div").getElementsByAttribute("data-level")
' Throw cake at the element
Next
我知道没有方法 getElementsByAttribute
,因此问题。
这里有我看不见的方法,还是我锁定手动迭代?
或者,如果我交换当前创建IE实例的方法,例如此答案,我是否可以方便地使用 querySelectorAll
最终得到类似于我上面概述的结果的东西?
Alternatively, if I swap my current approach for creating an IE instance, á la this answer, could I concievably use querySelectorAll
to end up with something resembling the result I have outlined above?
推荐答案
对于其他任何人这样,外壳可以说是这样:
For anyone else coming this way, the outer shell, so to speak, can look like this:
Sub ScrapeWithHTMLObj(url As String, domClassName As String, domTag As String, domAttribute As String, domAttributeValue As String)
' Dependencies:
' * Microsoft HTML Object Library
' Declare vars
Dim pHTML As HTMLDocument
Dim pElements As Object, pElement As Object
Set pHTML = New HTMLDocument
' Basic URL healthcheck
Do While (url = "" Or (Left(url, 7) <> "http://" And Left(url, 8) <> "https://"))
MsgBox ("Invalid URL!")
url = InputBox("Enter new URL: (0 to terminate)")
If url = "0" Then Exit Sub
Loop
' Fetch page at URL
With CreateObject("WINHTTP.WinHTTPRequest.5.1")
.Open "GET", url, False
.Send
pHTML.body.innerHTML = .ResponseText
End With
' Declare page elements
Set pElements = pHTML.getElementsByClassName(domClassName)
Set pElement = pElements(0).getElementsByTagName(domTag)
' Extract only elements with wanted attribute
pEleArray = getElementsByAttribute(pElement, domAttribute, domAttributeValue)
For Each e In pEleArray
' Do stuff to elements
Debug.Print e.getAttribute(domAttribute)
Next
End Sub
如果您选择这条路线,则还需要以下内容:
If you go this route, you'll also need something like this:
Function getElementsByAttribute(pObj As Object, domAttribute As String, domAttributeValue As String) As Object()
Dim oTemp() As Object
ReDim oTemp(1 To 1)
For i = 0 To pObj.Length - 1
'Debug.Print pObj(i).getAttribute(domAttribute)
If pObj(i).getAttribute(domAttribute) = domAttributeValue Then
Set oTemp(UBound(oTemp)) = pObj(i)
ReDim Preserve oTemp(1 To UBound(oTemp) + 1)
End If
Next i
ReDim Preserve oTemp(1 To UBound(oTemp) - 1)
getElementsByAttribute = oTemp
End Function
显然,根据HTML树,您需要更改子元素中置零的元素。对于我在测试中使用的网站,此结构完美运行。
Depending on the HTML tree, you'll need to change which elements you zero in on in the sub, obviously. For the site I used in testing, this structure worked flawlessly.
示例用法:
调用ScrapeWithHTMLObj( https: // somesite, chapter-index, div, data-level, 1)
它将输入第一个名为 chapter-index
的类,选择带有 div
标记的所有元素,最后提取包含该属性的所有元素数据级
,值 1
。
It will enter the first class named chapter-index
, select all elements with the div
tag, and finally extract all elements containing the attribute data-level
with value 1
.
这篇关于VBA / DOM-根据属性获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!