获取元素?标签名称id或类名称如何获得下面的innertext

时间:2017-07-10 18:17:58

标签: excel vba scrape innertext htmldoc

UPS网站已更改。而不是标签,它改为

Private Function TrackUPS(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 dds As Object  ' MSHTML.IHTMLElementCollection
    Dim ddr As Object
    Dim dt As Object
    Dim dd As Object  ' MSHTML.IHTMLElement

    'tempString = GetMSXMLWebResponse(UPSUrl & trackingNumber)

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

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

    If Len(tempString) = 0 Then
        TrackUPS = ERROR_MSG
        Exit Function
    End If

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

    Set htmlBody = htmlDoc.body
    htmlBody.innerHTML = tempString
    On Error Resume Next
    Set dds = htmlDoc.getElementsByclassname("").innerText
    'Set dds = htmlDoc.getElementsByTagName("dd")
    Set ddr = htmlDoc.getElementsByTagName("dt")
    Strg1 = htmlDoc.getElementById("tt_spStatus").innerText
    Strg2 = dds.Item(1).innerText
    Strg3 = dds.Item(11).innerText
    Strg4 = htmlDoc.getElementById("tt_pgfStatus").innerText
    Strg5 = htmlDoc.getElementById("tt_ovntStatus").innerText
    If Len(Strg1) = 0 Then
        Strg1 = Strg4
        If Len(Strg4) = 0 Then
            Strg1 = Strg5
        End If
    End If
    PODEnd10 = Strg1 & "|" & Strg2
    If PODEnd10 = "|1.   " Then GoTo Line1 Else GoTo Line2
    Line2:
    If PODEnd10 = "|>>>1." Then GoTo Line1 Else GoTo Line3
    Line3:
    TrackUPS = Strg1 & "|" & Strg2
    Exit Function
    Line1:
    TrackUPS = "NO|POD|INFO"
    Exit Function
End Function

因此网站显示日期和时间:

<p class="">
    Monday, &nbsp;01/11/2016
    at&nbsp;9:09 A.M.
</p>

我在上面的代码中使用它:     设置dds = htmlDoc.getElementsByclassname(“”)。innerText 但是<p class="">没有名字。如何捕获该元素的innertext? 任何帮助将不胜感激。我每天都依赖这个。

这是整个div类:

<div class="ups-group ups-group_condensed">
p class="ups-form_label"><strong>Delivered On:</strong></p>

<p class="">
    Monday, &nbsp;01/11/2016
    at&nbsp;9:09 A.M.
</p>
</div>

vba中的.innertext编码会返回星期一以及P级“”的日期和时间吗?

1 个答案:

答案 0 :(得分:0)

这有效:

Set dds = htmlDoc.getElementsByTagName("p")
Strg2 = dds.Item(11).innerText