VBA代码可刮取数据,而不是通过网页来回循环。还努力争取价值

时间:2019-05-14 07:13:12

标签: excel vba web-scraping

Good Day,我正在开发一种工具,可以从各种网站上扣除“单向费用”。对于特定的网站,我处于可以导航到正确页面的阶段,但是我无法弄清楚如何将单向费用转换为excel。我还有一个问题,我似乎无法单击“后退”按钮。我的代码“有效”之处在于,如果您通过按F8键来遍历脚本,那么您将到达所需的位置。如果您正常运行,它有时会落在“ carclick.item(x).click零件”上。请注意,单向费用并不总是出现。当接送地点不同时,它最有可能出现。我的代码需要执行的任务如下:

  1. 创建html对象,然后打开网站。
  2. 点击“将车辆返回到其他位置”按钮,以使代码可以访问其他下车位置。
  3. 由于它们不影响单程费用,并且网站默认值足够,所以我暂时忽略了接送时间。 (而且,我也无法使它正常工作)
  4. 然后代码输入两个位置:一个用于接送,一个用于送出。
  5. 然后,代码单击“查找车辆”按钮。该页面会加载一段时间。
  6. 此页面显示多个车辆选项。每个汽车街区可能都有一个需要点击的“书”或“仅请求”按钮(以蓝色突出显示),然后,将打开一个新页面,显示单向费用。 (我的代码中的示例值的值为1315.79)
  7. 然后,代码将需要对值进行转义并将其与汽车名称和汽车组(通常是字母)一起粘贴到excel中。
  8. 然后,我需要单击“后退”按钮以返回到车辆选择并针对所有汽车组重复该操作。

    1. 然后该工具需要对所有位置组合重复整个过程(即使对于相同的位置行程但方向相反,单向费用也可能有所不同)。我一直都在努力,但还没有达到目标。我确实需要有关步骤7和8的帮助。
    Private Sub test1()
    Dim appIE As Object
    Dim ws As Worksheet
    Dim wb As Workbook
    Dim a As String
    Dim b As String
    Dim c As String
    Dim x As Integer
    Dim d As String
    Dim e As Object
    Dim 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
    Dim Ret As Object
    Dim entry As Object
    Dim pd As Object
    Dim backbutton As Object
    Dim carclick  As Object
    
    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.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"))
    
    i = 2 'For i = 2 To 3
    With ws
    a = 1267
    d = 1261
    b = "15 - May - 19"
    c = "25 - May - 19"
    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
    
    Do While appIE.Busy And e Is Nothing
        DoEvents
    Application.Wait (Now + TimeValue("0:00:02"))
        Loop
    Application.Wait (Now + TimeValue("0:00:02"))
    ' 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
    
    Do While appIE.Busy And e Is Nothing
        DoEvents
    Application.Wait (Now + TimeValue("0:00:03"))
        Loop
    Application.Wait (Now + TimeValue("0:00:02"))
    
    '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
    
    Do While appIE.Busy And f Is Nothing
        DoEvents
    Application.Wait (Now + TimeValue("0:00:03"))
        Loop
    Do While appIE.Busy And l Is Nothing
        DoEvents
    Application.Wait (Now + TimeValue("0:00:01"))
        Loop
    Application.Wait (Now + TimeValue("0:00:01"))
    
    'Clicking find a vehicle
        For Each l In appIE.document.getElementsByTagName("input")
            If l.className = "btn" Then
                l.Click
        Exit For
            End If
            Next
    
    Application.Wait (Now + TimeValue("0:00:04"))
    ' On Error Resume Next
    Do While appIE.Busy
        Application.Wait (Now + TimeValue("0:00:04"))
        DoEvents
    Loop
    'While appIE.Busy Or appIE.readyState < 4: DoEvents: Wend
    
    Set carclick = appIE.document.querySelectorAll(".select-vehicle")
        For x = 1 To carclick.Length
            carclick.Item(x).Click
    
    'While appIE.Busy Or appIE.readyState < 4: DoEvents: Wend
    Application.Wait (Now + TimeValue("0:00:02"))
    ' On Error Resume Next
    Do While appIE.Busy
        Application.Wait (Now + TimeValue("0:00:02"))
        DoEvents
    Loop
    
    'This is where I tried to scrape the One-Way Fee value of 1315.79
    
    'ws.Cells(2, 3).Value = appIE.document.querySelector(".sidebar__list- item:nth-of-type(13) span:nth-of-type(1)").innerText
    
    Application.Wait (Now + TimeValue("0:00:02"))
    ' On Error Resume Next
    
    Do While appIE.Busy
        Application.Wait (Now + TimeValue("0:00:02"))
        DoEvents
    Loop
    
    Set backbutton = appIE.document.querySelectorAll(".back.btn")
        backbutton.Click
    While appIE.Busy Or appIE.readyState < 4: DoEvents: Wend
        Exit For
        Next x
    End With
    End Sub
    

0 个答案:

没有答案