【发布时间】:2020-09-04 13:55:27
【问题描述】:
我正在尝试创建一个用于抓取货物跟踪网站的宏。 但是我必须创建 4 个这样的宏,因为每家航空公司都有不同的网站。
我是 VBA 和网络抓取的新手。
我整理了一个适用于 1 个网站的代码。但是当我试图将它复制到另一个时,我陷入了困境。我想这可能是我指代元素的方式,但就像我说的,我是 VBA 新手,对 HTML 一无所知。
我正在尝试从图像中的突出显示行中获取“通知”值。
IMAGE:"notified" text to be extracted 下面是我到目前为止编写的代码,它卡在了循环中。 对此的任何帮助将不胜感激。
Sub FlightStat_AF()
Dim url As String
Dim ie As Object
Dim nodeTable 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 can do that in a loop to match the point the information is available
Do
On Error Resume Next
Set nodeTable = ie.document.getElementByClassName("block-whisper")
On Error GoTo 0
Loop Until Not nodeTable Is Nothing
'Get the status from the table
MsgBox Trim(nodeTable.getElementsByClassName("fs-12 body-font-bold").innerText)
'Clean up
ie.Quit
Set ie = Nothing
Set nodeTable = Nothing
End Sub
【问题讨论】:
-
您很可能陷入了一个循环,因为永远找不到“block-whisper”。在这种情况下,您的代码将永远循环。循环并不是真正需要的。您可以找到该元素,也可以不找到,您可以采取任何一种方式。
-
@BrianMStafford,需要循环,因为它是在加载源页面后加载的动态表。在元素部分,我不确定我是否指的是正确的元素,因为我对 HTML 一无所知。你能看看附上的图片,看看我犯了什么错误吗?
-
你尝试我的代码从这个答案到另一个页面:stackoverflow.com/questions/63738093/… 那行不通。每个页面的抓取代码都不同,因为每个页面都不同。我现在没有时间看看它在这个页面上是如何工作的。也许以后。
-
试试this link。你可以使用 xhr 来获取你想要的。
-
@SIM,谢谢。但我对网络抓取完全陌生,只有基本的 VBA 知识。你能帮我写代码吗?
标签: excel vba web-scraping