0

我不知道如何通过类名获取元素。我有以下标签。我可以通过执行 ("div") 并列出其项目编号来执行 getelementsbytagname,但我想在此实例中获取类名 item 2。在网页上,这里是标签。

<div class="column">

Projected Delivery Date:

</div>


<div class="column">

11/28/2017                                                                                                           
</div>

我正试图在那里找到日期。这是我用来完成此操作的代码。它不适用于“partner_info group”或“column”。我遍历所有标签,但似乎没有捡起它。

Private Function TrackNEW(trackingNumber As String) As String
Dim xml As Object
Dim tempString As String
Dim htmlDoc As Object  ' MSHTML.HTMLDocument
Dim htmlBody As Object  ' MSHTML.htmlBody
Dim anchors As Object  ' MSHTML.IHTMLElementCollection
Dim anchor As Object  ' MSHTML.IHTMLElement
Dim dda As Object  ' MSHTML.IHTMLElementCollection
Dim ddb As Object
Dim ddc As Object
Dim ddd As Object
Dim span As Object
Dim div As Object  ' MSHTML.IHTMLElement

Set xml = GetMSXML
If xml Is Nothing Then  ' cannot start MSXML 6.0
TrackNEW = MSXML_ERROR
Exit Function
End If

tempString = GetResponse(xml, HTTP_GET, NEWUrl & trackingNumber, False)

If Len(tempString) = 0 Then

TrackNEW = ERROR_MSG
Exit Function
End If

Set htmlDoc = CreateHTMLDoc
If htmlDoc Is Nothing Then  ' cannot reference MSHTML object library
TrackNEW = MSHTML_ERROR
Exit Function
End If

Set htmlBody = htmlDoc.body
htmlBody.innerHtml = tempString
On Error Resume Next

Set dda = htmlDoc.getElementsByTagName("span")
Set ddb = htmlDoc.getElementsByTagName("span")
Set ddc = htmlDoc.getElementsByTagName("span")
Set ddd = htmlDoc.getElementsByClassName("column")

For Each Strg4 In ddd
For ItemNumber4 = 1 To 600
Strg4 = ddd.Item(ItemNumber4).innerText
    If InStr(Strg4, "Projected Delivery Date:") >= 1 Then
    Strg4 = ddd.Item(ItemNumber4).innerText
    GoTo Line8
    Else
    End If
Next ItemNumber4
Next Strg4
MsgBox "Bad"
Exit Function
Line8:
TrackNEW = Strg4
Exit Function

如果有人可以解释如何修复我的代码,以便它会选择日期?我可以再次为“div”做getelementsbytagname,但如果可能的话,我希望它通过类名。我在 Excel 中使用 VBA 来执行此操作。这不是 Java 脚本

4

2 回答 2

1

不知道为什么你可以用 class of 循环所有元素column。当您找到具有Projected Delivery Dateas 值的项目时,然后在下一个索引处选择项目。我使用 CSS 类选择器来获取nodeList所有匹配的元素并循环它。

Dim nodeList As Object, i As Long
If htmlDoc Is Nothing Then Exit Sub
Set nodeList =htmlDoc.querySelectorAll(".column")
For i = 0 To nodeList.Length - 1
    If InStr(nodeList.item(i).innerText, "Projected Delivery Date") > 0 Then
        Debug.Print nodeList.item(i + 1).innerText
        Exit For
    End If
Next
于 2018-10-02T11:37:35.677 回答
0

好吧,我得到了这个工作,它使用 class="column" 读取元素的 innerText,所以看看你在做什么不同的事情并改变它,直到你可以让它工作。此外,您必须在工具>参考下将“Microsoft HTML 对象库”添加到项目中。

Sub sub1()
Dim hdoc As HTMLDocument

' your test HTML
sHtml = "<div class=""column"">" & vbCrLf & _
vbCrLf & _
"Projected Delivery Date:" & vbCrLf & _
vbCrLf & _
"</div>" & vbCrLf & _
vbCrLf & _
"<div class=""column"">" & vbCrLf & _
vbCrLf & _
"11/28/2017" & vbCrLf & _
"</div>"

Set hdoc = New HTMLDocument
hdoc.body.innerHTML = sHtml

For Each element In hdoc.getElementsByClassName("column")
    Debug.Print element.innerText
Next
End Sub
于 2017-11-28T00:20:19.837 回答