我该如何提取此文

时间:2016-05-10 19:21:47

标签: html vba excel-vba innertext excel

我正在查看此网页 - > https://www.fedex.com/apps/fedextrack/?action=track&tracknumbers=671299425542&locale=en_US&cntry_code=us

我希望将截止日期发布于2016年3月3日星期三下午12:16

到目前为止,这是我的代码

Public Sub FedExTracking()

Dim IE As Object
Dim ReturnValue As Object
Dim ProUrl As String
Dim RowCount As Integer
Dim PullText As String
Dim iCounter As Integer

Set IE = CreateObject("InternetExplorer.application")

RowCount = 0

Do While Not ActiveCell.Offset(RowCount, -1).Value = ""

ProUrl = "https://www.fedex.com/apps/fedextrack/?action=track&tracknumbers=" & ActiveCell.Offset(RowCount, -1).Value & "&locale=en_US&cntry_code=us"

With IE
    .Visible = True
    .Navigate ProUrl
    Do Until Not IE.Busy And IE.readyState = 4: DoEvents: Loop
End With


iCounter = 0
Do While iCounter < 8
    WaitHalfSec
    iCounter = iCounter + 1
Loop


set ReturnValue = IE.document.getElementsClassName("snapshotController_date.dest")(0)

'THIS LINE RETURNS RUN TIME ERROR "91" OBJECT VARIABLE OR WITH BLOCK VARIABLE NOT SET
PullText = ReturnValue.innertext

ActiveCell.Offset(RowCount).Value = PullText & "."

RowCount = RowCount + 1

Loop

IE.Quit
Set IE = Nothing

End Sub

Sub WaitHalfSec()
Dim t As Single
t = Timer + 1 / 2
    Do Until t < Timer: DoEvents: Loop
End Sub

只要我没有找到innertext,我就能找到它并找到存储线。我将如何在这条线上返回日期?

&LT; div class =&#34; snapshotController_date.dest&#34; &GT; 2016年3月3日星期三12:16&lt; / div&gt;

感谢任何帮助!

2 个答案:

答案 0 :(得分:2)

请改为尝试:

ReturnValue = IE.document.getElementsByClassName("snapshotController_date.dest")(0).innerText

即使类名在检查时看起来有空格,但如果查看DOM资源管理器,它会有一个句点。它也是类名,而不是ID,所以你需要使用getElementsByClassName()方法并使用它返回的HTMLCollection对象。

答案 1 :(得分:0)

我能够让这个工作。只需将答案提供给任何需要它的人。

Public Sub FedExTrackingWorking()
Dim ie As Object
Dim ProURL As String
Dim iCounter As Integer
Dim htmlColl As MSHTML.IHTMLElementCollection
Dim htmlInput As MSHTML.HTMLInputElement
Dim RowCount As Integer

RowCount = 0

Set ie = CreateObject("InternetExplorer.application")

Do While Not ActiveCell.Offset(RowCount, -1).Value = ""

ProURL = "https://www.fedex.com/apps/fedextrack/?action=track&tracknumbers=" & ActiveCell.Offset(RowCount, -1).Value & "&locale=en_US&cntry_code=us"

With ie
    .Visible = True
    .navigate ProURL
    Do Until Not ie.Busy And ie.readyState = 4: DoEvents: Loop
End With

iCounter = 0
Do While iCounter < 8
    WaitHalfSec
    iCounter = iCounter + 1
Loop

Set htmlColl = ie.document.getElementsByTagName("div")

For Each htmlInput In htmlColl
    If htmlInput.className = "snapshotController_date dest" Then
        ActiveCell.Offset(RowCount).Value = htmlInput.innerText
        Exit For
    End If
Next htmlInput

RowCount = RowCount + 1

Loop

ie.Quit
Set ie = Nothing

End Sub

Sub WaitHalfSec()
    Dim t As Single
    t = Timer + 1 / 2
        Do Until t < Timer: DoEvents: Loop
End Sub