使用类名重复的VBA搜寻网站的值

时间:2019-05-06 12:15:51

标签: html vba web-scraping

好,这个问题分为两个部分。

  1. 我需要使用VBA将网站上的值R1200刮入Excel。我尝试了各种方法,但似乎无法正确解决。 @QHarr提供了有效的解决方案,但我认为网站的格式已更改。
  2. 当我使用键盘按钮F8单步执行代码时,该代码就会运行,但是当我正常运行它时,代码就会掉落。

部分:

  1. 这是存储值R1200的HTML,“ clearfix extras”元素不是唯一的,我要的是下面显示的第4个:

<ul class="clearfix extras">
    <li>
        Document admin fee<span>R 99.00</span>
    </li>
    <li>
        Vehicle Rental <span>R 2870.00</span>
    </li>
    <li>
        <!--This is part of the temporary solution to show the oneway surcharge-->
        One Way Drop Off Surcharge<span>R 1200.00</span>
    </li>
</ul>

以下是我尝试过的所有内容,是否可以使用查询选择器来识别“ clearfix extras”的第四个实例?

`Cells(r, 3).Value = appIE.document.getElementsByClassName("thank-you-message box-shadow-dark-3").getElementsByClassName("optional-extras").querySelector(".clearfix.extras li:nth-of-type(3) span").innerText
Cells(r, 3).Value = appIE.document.getElementsByClassName("thank-you-message box-shadow-dark-3").getElementsByClassName("itinerary wide").getElementsByClassName("itinerary-container clearfix").querySelector(".optional.extras li:nth-of-type(3) span").innerText
Cells(r, 4).Value = appIE.document.querySelector(".clearfix.extras li:nth-of-type(3) span").innerText
Cells(r, 5).Value = appIE.document.getElementsByClassName("thank-you-message box-shadow-dark-3").getElementsByClassName("itinerary wide").getElementsByClassName("itinerary-container clearfix").querySelector(".clearfix.extras li:nth-of-type(3) span").innerText
Cells(r, 6).Value = appIE.document.querySelector(".thank-you-message box-shadow-dark-3.itinerary.wide.itinerary-container.clearfix.itinerary-container.clearfix li:nth-of-type(3) span").innerText
Cells(r, 7).Value = appIE.document.querySelector(".clearfix.extras:nth-of-type(3) li:nth-of-type(3) span").innerText
Cells(r, 3).Value = appIE.document.getElementsByClassName("thank-you-message box-shadow-dark-3").getElementsByClassName("itinerary wide").getElementsByClassName("itinerary-container clearfix").querySelector(".optional.extras li:nth-of-type(3) span").innerText
Set OWF = appIE.document.getElementsByClassName("thank-you-message box-shadow-dark-3")
Cells(r, 3).Value = OWF.querySelector("li:nth-of-type(2) span").innerText
Cells(r, 5).Value = appIE.document.getElementsByClassName("thank-you-message box-shadow-dark-3").getElementsByClassName("itinerary wide").getElementsByClassName("itinerary-container clearfix").querySelector(".clearfix.extras li:nth-of-type(3) span").innerText
Cells(r, 6).Value = appIE.document.querySelector(".thank-you-message box-shadow-dark-3.itinerary.wide.itinerary-container.clearfix.itinerary-container.clearfix li:nth-of-type(3) span").innerText
Cells(r, 7).Value = appIE.document.querySelector(".clearfix.extras:nth-of-type(3) li:nth-of-type(3) span").innerText`
  1. 我一直在使用下面的代码(我的完整代码),但是我发现我的代码一直都存在于不同的地方。有一个更好的方法吗?不幸的是,我的代码必须浏览许多页面(并填写详细信息)才能获得我所需的R1200值。也许我的app.wait部件使用不正确?我知道代码不完整,我需要弄清楚如何使它循环到所有汽车,而不仅仅是“起亚Picanto”。我只是想弄清楚如何将我的问题的第1部分和第2部分暂时排序。谢谢

代码:

Private Sub test1()
    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("Test2")
    Set ws = wb.Worksheets("Sheet1")
    Set appIE = CreateObject("internetexplorer.application")

    With appIE
        .navigate "https://www.europcar.co.za"
        .Visible = True


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

        For i = 2 To 2
            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:02"))
            Loop
            Application.Wait (Now + TimeValue("0:00:02"))

            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:03"))
            Loop
            Application.Wait (Now + TimeValue("0:00:02"))

            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:03"))
            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")
            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")
            For Each Ret In appIE.document.getElementsByName("ReturnDate")
                Ret.Value = c
            Next Ret

            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:02"))
            On Error Resume Next
            Do While appIE.Busy
                Application.Wait (Now + TimeValue("0:00:03"))
                DoEvents
            Loop

            Application.Wait (Now + TimeValue("0:00:02"))

            For Each k In appIE.document.getElementsByClassName("filtered-vehicles")(0).getElementsByClassName("vehicle box-shadow-dark-2").getElementsByClassName("KIA PICANTO")

                For Each l In appIE.document.getElementsByClassName("select-btn btn grey")
                    If l.className = "select-btn btn grey" Then
                        l.Click

                        Exit For
                    End If
                    Application.Wait (Now + TimeValue("0:00:02"))
                    Exit For
                Next

            Next

            For Each q In appIE.document.getElementsByClassName("btn search-btn")
                If q.className = "btn search-btn" Then
                    q.Click
                    Exit For
                End If
            Next


            For Each Z In appIE.document.getElementsByClassName("btn search-btn")
                If Z.className = "btn search-btn" Then
                    Z.Click
                    Exit For
                End If
            Next

            Application.Wait (Now + TimeValue("0:00:02"))

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

            .document.getElementById("TitleID").Value = "8"

            appIE.document.all.item("step4-initials").Value = "U"
            appIE.document.all.item("step4-first-name").Value = "Underhill"
            appIE.document.all.item("step4-surname").Value = "Tsonga"
            appIE.document.all.item("step4-surname").Value = "Tsonga"
            appIE.document.all.item("step4-email").Value = "Car@Check.com"
            appIE.document.all.item("step4-contact-num").Value = "0915598452"
            appIE.document.all.item("step4-id-number").Value = "0112022374"

            Application.Wait (Now + TimeValue("0:00:04"))
            Application.ScreenUpdating = True
            On Error Resume Next
            Do While appIE.Busy
                Application.Wait (Now + TimeValue("0:00:03"))
                DoEvents
            Loop

            appIE.document.getElementById("terms_and_conditions").Click

            For Each Z In appIE.document.getElementsByClassName("btn search-btn")
                If Z.className = "btn search-btn" Then
                    Z.Click
                    Exit For
                End If
            Next Z

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

            Cells(r, 1).Value = Mid(appIE.document.querySelector(".vehicle-information h5:nth-of-type(1) ").innerText, 7, 1)
            Cells(r, 2).Value = Mid(appIE.document.querySelector(".vehicle-information h5:nth-of-type(1) ").innerText, 8, 16)
            Cells(r, 3).Value = appIE.document.getElementsByClassName("thank-you-message box-shadow-dark-3").getElementsByClassName("optional-extras").querySelector(".clearfix.extras li:nth-of-type(3) span").innerText
            Cells(r, 3).Value = appIE.document.getElementsByClassName("thank-you-message box-shadow-dark-3").getElementsByClassName("itinerary wide").getElementsByClassName("itinerary-container clearfix").querySelector(".optional.extras li:nth-of-type(3) span").innerText
            Cells(r, 4).Value = appIE.document.querySelector(".clearfix.extras li:nth-of-type(3) span").innerText
            Cells(r, 5).Value = appIE.document.getElementsByClassName("thank-you-message box-shadow-dark-3").getElementsByClassName("itinerary wide").getElementsByClassName("itinerary-container clearfix").querySelector(".clearfix.extras li:nth-of-type(3) span").innerText
            Cells(r, 6).Value = appIE.document.querySelector(".thank-you-message box-shadow-dark-3.itinerary.wide.itinerary-container.clearfix.itinerary-container.clearfix li:nth-of-type(3) span").innerText
            Cells(r, 7).Value = appIE.document.querySelector(".clearfix.extras:nth-of-type(3) li:nth-of-type(3) span").innerText
            Cells(r, 3).Value = appIE.document.getElementsByClassName("thank-you-message box-shadow-dark-3").getElementsByClassName("itinerary wide").getElementsByClassName("itinerary-container clearfix").querySelector(".optional.extras li:nth-of-type(3) span").innerText
            Set OWF = appIE.document.getElementsByClassName("thank-you-message box-shadow-dark-3")
            Cells(r, 3).Value = OWF.querySelector("li:nth-of-type(2) span").innerText
            Cells(r, 5).Value = appIE.document.getElementsByClassName("thank-you-message box-shadow-dark-3").getElementsByClassName("itinerary wide").getElementsByClassName("itinerary-container clearfix").querySelector(".clearfix.extras li:nth-of-type(3) span").innerText
            Cells(r, 6).Value = appIE.document.querySelector(".thank-you-message box-shadow-dark-3.itinerary.wide.itinerary-container.clearfix.itinerary-container.clearfix li:nth-of-type(3) span").innerText
            Cells(r, 7).Value = appIE.document.querySelector(".clearfix.extras:nth-of-type(3) li:nth-of-type(3) span").innerText

            .navigate "https://www.europcar.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 + 1

        Next i

    End With
    appIE.Quit
    Set appIE = Nothing

End Sub

1 个答案:

答案 0 :(得分:0)

很难没有测试值进入网站以生成您希望抓取的值。请提供这些。

您提到的那个值的选择器是:

.clearfix.extras li:nth-of-type(3) span

尽管这将选择类名称为clearfix extras的第一个元素,所以您可能需要其他索引。

第四个匹配类名称的示例:

.clearfix.extras:nth-of-type(4) li:nth-of-type(3) span