html - 我该如何提取这段文字

标签 html vba excel innertext

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

我想返回交货日期 2016 年 3 月 31 日星期四中午 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

只要我不费劲去寻找内部文本,我就能够找到并存储该行。我该如何返回这条线的日期?

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

感谢任何帮助!

最佳答案

试试这个:

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

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

关于html - 我该如何提取这段文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37147209/

相关文章:

javascript - 如何在 javascript/node.js 中的 HTML tablesorter 表体中插入行?

javascript - 无法调用函数

MySQL 到 Excel VBA

algorithm - 尝试算法的媒介?

excel - 具有参数的Excel自定义函数

jquery - 使用 jQuery 防止点击第二个按钮

javascript - 如何在 Google Analytics 的自定义变量中使用变量

vba - 导入/导出关系

excel - Excel更改链接到加载项

javascript - 使用 java/javascript 和 apache POI 导出 .xls 文件时文件损坏