为什么我的macros不像预期的那样刮?

我正在使用下面的macros来刮一个网站。 当我使用F8运行macros时,它运行正常,但是当我正常运行它时,它跳过了在这些行中的信息:

Cells(r, 1).Value = entry.getAttribute("data-vehiclegroup") Cells(r, 2).Value = entry.getAttribute("data-vehicletransmission") Cells(r, 3).Value = entry.getAttribute("data-vehicletitle") Cells(r, 4).Value = entry.getAttribute("datstandardwaiverratefee") Cells(r, 5).Value = entry.getAttribute("data-superwaiverratefee") r = r + 1 

当i = 1,3和5时,这个macros跳过了抓取信息。我不明白为什么要这么做或者如何解决这个问题。 任何帮助,将不胜感激。

  Sub car() Dim appIE As Object Dim e As Object Dim ws As Worksheet Dim wb As Workbook Dim o Dim a As String Dim b As String Dim c As String Dim d As String Dim PickUp As Object Dim iL As IHTMLElement Dim f As IHTMLElementCollection Dim post As Object Dim Ret As Object Dim entry As Object Dim l As Object r = 2 Set wb = Application.Workbooks("car3") Set ws = wb.Worksheets("Sheet1") 'Open internet explorer Set appIE = CreateObject("internetexplorer.application") 'Navigate to site With appIE .Navigate "https://www.car.co.za" .Visible = True Do While appIE.busy DoEvents Application.Wait (Now + TimeValue("0:00:03")) Loop Application.Wait (Now + TimeValue("0:00:01")) For i = 1 To 6 With ws a = .Cells(i, 8) d = .Cells(i, 9) b = .Cells(i, 10) c = .Cells(i, 11) End With Do While appIE.busy And e Is Nothing DoEvents Application.Wait (Now + TimeValue("0:00:01")) Loop Application.Wait (Now + TimeValue("0:00:01")) Set e = appIE.document.getElementById("PickupBranch_BranchID_id") For Each o In e.Options If o.Value = a Then o.Selected = True Exit For End If Next Do While appIE.busy And e Is Nothing DoEvents Application.Wait (Now + TimeValue("0:00:01")) Loop Set e = appIE.document.getElementById("ReturnBranch_BranchID_id") For Each o In e.Options If o.Value = d Then o.Selected = True Exit For End If Next Do While appIE.busy And f Is Nothing DoEvents Application.Wait (Now + TimeValue("0:00:02")) Loop Set f = appIE.document.getElementById("timepicker-pickup").getElementsByTagName("li") For Each iL In f If iL.innerText = "09" Then iL.Click Exit For End If Next iL Do While appIE.busy And post Is Nothing DoEvents Application.Wait (Now + TimeValue("0:00:02")) Loop Set post = appIE.document.getElementsByName("PickupDate") 'Looping through multiple values: For Each post In appIE.document.getElementsByName("PickupDate") post.Value = b Next post Do While appIE.busy And Ret Is Nothing DoEvents Application.Wait (Now + TimeValue("0:00:02")) Loop Set Ret = appIE.document.getElementsByName("ReturnDate") 'Looping through multiple values: For Each Ret In appIE.document.getElementsByName("ReturnDate") Ret.Value = c Next Ret 'Clicking Book now button Do While appIE.busy And l Is Nothing DoEvents Application.Wait (Now + TimeValue("0:00:04")) Loop Application.Wait (Now + TimeValue("0:00:03")) For Each l In appIE.document.getElementsByClassName("btn search-btn") If l.className = "btn search-btn" Then l.Click Exit For End If Next Application.Wait (Now + TimeValue("0:00:01")) On Error Resume Next Do While appIE.busy And entry Is Nothing Application.Wait (Now + TimeValue("0:00:05")) DoEvents Loop 'Scraping information For Each entry In appIE.document.getElementsByClassName("filtered-vehicles")(0).getElementsByClassName("vehicle box-shadow-dark-2") Cells(r, 6).Value = i Cells(r, 1).Value = entry.getAttribute("data-vehiclegroup") Cells(r, 2).Value = entry.getAttribute("data-vehicletransmission") Cells(r, 3).Value = entry.getAttribute("data-vehicletitle") Cells(r, 4).Value = entry.getAttribute("data-standardwaiverratefee") Cells(r, 5).Value = entry.getAttribute("data-superwaiverratefee") r = r + 1 Next entry .Navigate "https://www.car.co.za" .Visible = True Application.Wait (Now + TimeValue("0:00:01")) Do While appIE.busy DoEvents Application.Wait (Now + TimeValue("0:00:03")) Loop r = r + 2 Next i End With appIE.Quit Set appIE = Nothing End Sub 

在这个循环中你不需要application.wait;

 Do While appIE.busy DoEvents Application.Wait (Now + TimeValue("0:00:03")) Loop 

它会等到站点完全加载。 至于你的问题,我猜想,当你的macros在做这个工作时,网站会触发一些脚本。 你可以在你的'For Each entry'循环或者Application.Wait(Now + TimeValue(“0:00:05”))之间添加Do While AppIE.busy Loop。

经过大量的实验后,我发现把–Application.Wait(Now + TimeValue(“0:00:05”))放在每个条目之前sorting问题out1