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

时间:2017-11-22 11:18:29

标签: excel vba web-scraping

我正在使用下面的宏来抓取一个网站。当我使用F8运行宏时,它运行正常,但是当我正常运行它时,它会跳过这些行的信息: -

            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时,宏会跳过抓取信息。我不明白为什么会这样做或如何修复它。任何帮助将不胜感激。

 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

2 个答案:

答案 0 :(得分:1)

在此循环中不需要application.wait;

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

它将等到网站完全加载。关于你的问题,我想当你的宏正在完成工作时,网站会激活一些脚本。您可以在“For Each entry”循环或Application.Wait(Now + TimeValue(“0:00:05”))之间添加Do While appIE.busy循环。

答案 1 :(得分:0)

经过多次实验,我发现放: - Application.Wait(Now + TimeValue(“0:00:05”))之前的For条目排序问题out1