【问题标题】:How would I pull this text我将如何提取此文本
【发布时间】:2016-09-05 22:39:34
【问题描述】:

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

我想退回交货日期 Thu 3/31/2016 12:16 pm

这是我目前的代码

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

只要我不想进入内部文本,我就能够找到并存储似乎的行。我将如何在这条线上返回日期?

2016 年 3 月 31 日星期四下午 12:16

感谢任何帮助!

标签: html vba excel innertext


【解决方案1】:

试试这个:

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

即使在检查时类名似乎有一个空格,但如果你在 DOM 资源管理器中查看,它会在那里有一个句点。此外,它是类名,而不是 ID,因此您需要使用 getElementsByClassName() 方法并使用它返回的 HTMLCollection 对象。

【讨论】:

  • 您能否向我解释一下“使用它返回的 HTMLCollection 对象”是什么意思。我见过一些其他的例子,比如 HTMLObjectElement,但这似乎不像我的 excel 上的类型。我的参考文献中确实有 html 对象库。
  • getElementsByClassName() 方法返回元素的collection(有点像数组),因为可能有多个元素具有此类。因此,您要么必须遍历集合并找到您要查找的项目 - 或者在这种情况下,它看起来像该类的第一个元素,所以我在方法之后使用 (0) 直接访问它.这有帮助吗?
  • 有道理,谢谢。我正在为在同一行返回的另一个错误编辑原始问题。
【解决方案2】:

我能够得到这个工作。只需将答案发布给任何需要它的人。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多