我已经在VBA中开发了一个Web抓取代码,但是在导航到所需数据时遇到了麻烦

时间:2019-05-10 13:56:03

标签: html vba web-scraping

代码导航到我需要访问的页面之前的页面。我需要点击“仅请求”按钮。

代码需要执行以下操作:

访问Hertz网站,输入接送地点,设置接送日期。然后点击查找车辆。然后,需要选择车辆,然后取消显示的“单向费用”。总体而言,我需要代码来针对所有位置组合和所有可用的汽车组执行此操作。出于这个问题的目的,我只想帮助您了解如何进入包含“单向”费用的页面,然后将其值提取为excel。然后,我将尝试弄清楚如何使其循环。

我尝试使用类名,但是没有运气。我不得不缩短代码以使其适合,但它似乎仍然可以正常工作。

Private Sub test1()
    Dim appIE As Object
    Dim ws As Worksheet
    Dim wb As Workbook
    Dim a As String, b As String, c As String, d As String, e As Object, l As Object
    Dim PickUp As Object
    Dim iL As IHTMLElement                       'this declares the html object
    Dim f As IHTMLElementCollection              ' this declares the collection of html objects
    Dim post As Object, Ret As Object, entry As Object
    r = 2                                        ' sets the start row of where to input the One Way fee etc
    Set wb = Application.Workbooks("Hertz")
    Set ws = wb.Worksheets("One Way Fees")
    Set appIE = CreateObject("internetexplorer.application")
    With appIE
        .Navigate "https://www.Hertz.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"))
        Set g = appIE.document.getElementById("return-location")
        g.Click
        Application.ScreenUpdating = True
        'this part sets the station in and station out cells as well as the pickup/dropoff dates
        i = 2                                    'For i = 2 To 3
        With ws
            a = 1267
            '.Cells(i, 8)
            d = 1261
            '.Cells(i, 9)
            b = "15 - May - 19"
            '.Cells(i, 10)
            c = "25 - May - 19"
            '.Cells(i, 11)
        End With
        For Each g In appIE.document.getElementsByClassName("return-location")
            If g.className = "return-location" Then
                g.Click
                Exit For
            End If
        Next g
        ' finds the pickup branch in html and clicks selection
        Set e = appIE.document.getElementById("pickup-depot")
        For Each O In e.Options
            If O.Value = a Then
                O.Selected = True
                Exit For
            End If
        Next
        'sets the return branch and clicks the selection
        Set e = appIE.document.getElementById("return-depot")
        For Each O In e.Options
            If O.Value = d Then
                O.Selected = True
                Exit For
            End If
        Next
        Set post = appIE.document.getElementsByName("pickup-pate")
        For Each post In appIE.document.getElementsByName("PickupDate")
            post.Value = b
        Next post
        ' sets the return date and clicks the button
        Set Ret = appIE.document.getElementsByName("return-date")
        For Each Ret In appIE.document.getElementsByName("return-date")
            Ret.Value = c
        Next Ret
        'Clicking find a vehicle
        For Each l In appIE.document.getElementsByTagName("input")
            If l.className = "btn" Then
                l.Click
                Exit For
            End If
        Next
        'This is the part where I would need to click the request button to select a vehicle. After this I would need the One Way fee.
        'Next
    End With
End Sub

1 个答案:

答案 0 :(得分:1)

不确定哪一种方法只是请求,您可以使用classname作为CSS选择器

.select-vehicle

VBA:

Dim requests As Object
Set requests = ie.document.querySelectorAll(".select-vehicle")
requests.Item(1).Click '2nd in list

上面是节点列表,其中包含所有可以索引到请求的按钮