首页 > 解决方案 > 我在 VBA 中开发了一个网页抓取代码,但无法导航到我需要的数据

问题描述

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

本准则需要执行以下操作:

访问 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

标签: htmlvbaweb-scraping

解决方案


不确定一种方法在哪里,但仅用于请求,您可以使用类名作为 css 选择器

.select-vehicle

VBA:

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

以上是您可以索引的请求的所有按钮的节点列表


推荐阅读