一些基础知识:
对于像现在这样的简单访问,您可以使用 DOM(文档对象模型)的 get 方法。但两者之间有一个重要的区别getElementByID()
and getElementsByClassName()
/ getElementsByTagName()
.
getElementByID()
搜索 html 标签的唯一 ID。这被写为 html 标签的 ID 属性。如果页面保留 html 标准,则只有一个元素具有此唯一 ID。这就是该方法开头的原因getElement
.
如果使用该方法时未找到 ID,VBA 将引发运行时错误。因此,该调用被封装在我的另一个答案的循环中,关闭并再次打开错误处理。但在这个问题的页面中,没有相关 html 区域的 ID。
相反,可以直接访问所需的元素。您尝试了访问getElementsByClassName()
。这是正确的。但区别就在这里getElementByID()
.
getElementsByClassName()
and getElementsByTagName()
首先getElements
。这是复数,因为可以有任意多个具有相同类或标签名称的元素。这两种方法都会创建一个 html 节点集合。所有具有所要求的类或标签名称的 html 元素都将在这些集合中列出。
所有元素都有一个索引,就像数组一样。索引从 0 开始。要访问特定元素,必须指定所需的索引。两个类名fs-12 body-font-bold
(类名之间用空格分隔,也可以只使用一个类名构建一个节点集合)向节点集合传递2个html元素。您想要第二个,因此必须使用索引 1。
这是使用 IE 查询页面的 VBA 代码:
Sub FlightStat_AF()
Dim url As String
Dim ie As Object
'You can handle the parameters id and pfx in a loop to scrape dynamic numbers
url = "https://www.afklcargo.com/mycargo/shipment/detail/057-92366691"
'Initialize Internet Explorer, set visibility,
'call URL and wait until page is fully loaded
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
ie.navigate url
Do Until ie.readyState = 4: DoEvents: Loop
'Wait to load dynamic content after IE reports it's ready
'We do that with a fix manual break of a few seconds
'because the whole page will be "reload"
'The last three values are hours, minutes, seconds
Application.Wait (Now + TimeSerial(0, 0, 3))
'Get the status from the table
MsgBox Trim(ie.document.getElementsByClassName("fs-12 body-font-bold")(1).innerText)
'Clean up
ie.Quit
Set ie = Nothing
End Sub
编辑:子函数
这个子测试功能:
Sub testFunction()
Dim flightStatAfResult As String
flightStatAfResult = FlightStat_AF("057-92366691")
MsgBox flightStatAfResult
End Sub
这是子函数:
Function FlightStat_AF(cargoNo As String) As String
Dim url As String
Dim ie As Object
Dim result As String
'You can handle the parameters id and pfx in a loop to scrape dynamic numbers
url = "https://www.afklcargo.com/mycargo/shipment/detail/" & cargoNo
'Initialize Internet Explorer, set visibility,
'call URL and wait until page is fully loaded
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
ie.navigate url
Do Until ie.readyState = 4: DoEvents: Loop
'Wait to load dynamic content after IE reports it's ready
'We do that with a fix manual break of a few seconds
'because the whole page will be "reload"
'The last three values are hours, minutes, seconds
Application.Wait (Now + TimeSerial(0, 0, 3))
'Get the status from the table
result = Trim(ie.document.getElementsByClassName("fs-12 body-font-bold")(1).innerText)
'Clean up
ie.Quit
Set ie = Nothing
'Return value of the function
FlightStat_AF = result
End Function