从网站表中提取一个数字

时间:2015-01-25 20:58:13

标签: excel vba excel-vba

我目前正在尝试使用VBA来抓取某个网站上特定表格中的特定数字。下面是我浏览器中 inspect element 面板的HTML代码。

<tr class="cmeRowBandingOff cmeTableRowHighlight">
        <th scope="row">MAR 15</th>
        <td>2056.50</td>
        <td>2062.50</td>
        <td>2042.25</td>
        <td>2043.25</td>
        <td><span>-12.50</span></td>
        <td>2044.00</td>
        <td class="cmeTableRight">1,351,989</td>
        <td class="cmeTableRight">2,701,326</td>
    </tr>

我编写的VBA代码将提取整个表格,从“MAR 15”一直到“2,701,326” - 但是我只希望将数字“2044.00”提取到excel中的单元格/消息框中。 / p>

我目前的代码如下:

Private Sub CommandButton1_Click()
    Dim IE As Object
    ' Create InternetExplorer Object
    Set IE = CreateObject("InternetExplorer.Application")
    ' You can uncoment Next line To see form results
    IE.Visible = False
    ' URL to get data from
    IE.Navigate "http://www.cmegroup.com/trading/equity-index/us-index/" _
        & "e-mini-sandp500_quotes_settlements_futures.html"
    ' Statusbar
    Application.StatusBar = "Loading, Please wait..."
    ' Wait while IE loading...
    Do While IE.Busy
        Application.Wait DateAdd("s", 5, Now)
    Loop
    Application.StatusBar = "Searching for value. Please wait..."
    Dim dd As String
    dd = IE.Document.getElementsByClassName("cmeRowBandingOff")(0).innerText
    MsgBox dd
    ' Show IE
    IE.Visible = True
    ' Clean up
    Set IE = Nothing
    Application.StatusBar = ""
End Sub

我知道我需要改变这个:

dd = IE.Document.getElementsByClassName("cmeRowBandingOff")(0).innerText

但我不确定如何去做 有人可以帮助我改变VBA代码以获得 2044.00 的结果吗?

1 个答案:

答案 0 :(得分:2)

类似的东西:

Dim rw, dd

Set rw = IE.Document.getElementsByClassName("cmeRowBandingOff")(0)

dd = rw.getElementsByTagName("td")(5).innerText
'or
dd = rw.childNodes(6).innerText
'or
dd = rw.Cells(6).innerText