如何操作网页上的多个按钮单击

时间:2019-06-02 16:53:55

标签: html excel vba internet-explorer web-scraping

当前从网页(https://www.oddschecker.com/golf/memorial-tournament/winner)提取数据。

在提取数据之前,我希望数据的格式为十进制,而不是小数。可以通过单击页面右上角的“滚轮”,然后单击“十进制”来完成。

VBA在代码内没有出现错误,但是,一旦将数据传输到excel工作表中,数据将保留为小数格式。

任何对此的想法将不胜感激。

Sub ScrapeOdds()

    Dim IE As New SHDocVw.InternetExplorer
    Dim HTMLDoc As MSHTML.HTMLDocument
    Dim HTMLTable As MSHTML.IHTMLElement
    Dim HTMLDiv As MSHTML.IHTMLElement
    Dim InitialClick As Object
    Dim FinalClick As Object

    IE.Visible = True
    IE.navigate "https://www.oddschecker.com/golf/memorial-tournament/winner"

    Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy
    Loop

    Set HTMLDoc = IE.document
    Set HTMLDiv = HTMLDoc.getElementById("oddsTableContainer")
    Set HTMLTable = HTMLDiv.getElementsByTagName("table")(0)

    Set InitialClick = IE.document.getElementsByClassName("icon-item all-tools header-button")(0).Click
    Set FinalClick = IE.document.getElementsByClassName("OddsSettingsController as OddsSettingsController")

        For Each Obj In FinalClick

        If FinalClick.getAttribute("ng-click") = ("OddsSettingsController.setOddsType('decimal')") Then

        FinalClick.Click

        Exit For
        End If
        Next


    WriteTableToSheet HTMLTable

    IE.Quit

End Sub

1 个答案:

答案 0 :(得分:1)

您可以如下所示单击。您感兴趣的行如下:

With .document
    If .querySelectorAll(".offer-close").Length > 0 Then .querySelector(".offer-close").Click
    .querySelector(".tools-icon").Click
    If .querySelectorAll("[title='Change to decimal odds']").Length > 0 Then .querySelector("[title='Change to decimal odds']").Click
End With

这会检查商品弹出广告是否存在并关闭:

If .querySelectorAll(".offer-close").Length > 0 Then .querySelector(".offer-close").Click

这将打开轮子:

.querySelector(".tools-icon").Click

这将检查当前是否选择了十进制,如果未选择,则选择

If .querySelectorAll("[title='Change to decimal odds']").Length > 0 Then .querySelector("[title='Change to decimal odds']").Click

下面的代码删除了开始时表中的一些垃圾。


VBA

Option Explicit

'VBE > Tools > References: Microsoft Internet Controls; HTML Object Library
Public Sub GetData()
    Dim ie As Object, hTable As HTMLTable, clipboard As Object, ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Set ie = CreateObject("InternetExplorer.Application")
    Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    With ie
        .Visible = True
        .Navigate2 "https://www.oddschecker.com/golf/memorial-tournament/winner"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document
            If .querySelectorAll(".offer-close").Length > 0 Then .querySelector(".offer-close").Click
            .querySelector(".tools-icon").Click
            If .querySelectorAll("[title='Change to decimal odds']").Length > 0 Then .querySelector("[title='Change to decimal odds']").Click
        End With
        While .Busy Or .readyState < 4: DoEvents: Wend
        Set hTable = .document.querySelector(".eventTable")
        clipboard.SetText hTable.outerHTML
        clipboard.PutInClipboard
        ws.Range("A1").PasteSpecial
        Dim cutOff As Range
        Set cutOff = ws.Columns(1).Find("QuickBet")
        If Not cutOff Is Nothing Then ws.rows("1:" & cutOff.Row).EntireRow.Delete
        .Quit
    End With
End Sub