【问题标题】:Extract innerText from one cell从一个单元格中提取innerText
【发布时间】:2013-07-16 11:01:50
【问题描述】:

我正在尝试仅提取 HTML 表中行中最右侧单元格的内部文本。这是 HTML 代码的一小部分。该行包含 810 个单元格,TR 标签包含 811 个 TD 标签:

</tr><tr align="center" id="spt_inner_row_2"><td nowrap="nowrap" bgcolor="#EEEEEE" style="border-bottom: 1px solid white; border-right: 1px solid white">
&nbsp;300 - 305&nbsp;
</td><td nowrap="nowrap" bgcolor="#EEEEEE" style="border-bottom: 1px solid white; border-right: 1px solid white">
&nbsp;300 - 305&nbsp;
</td><td nowrap="nowrap" bgcolor="#EEEEEE" style="border-bottom: 1px solid white; border-right: 1px solid white">
&nbsp;300 - 305&nbsp;
</td><td nowrap="nowrap" bgcolor="#EEEEEE" style="border-bottom: 1px solid white; border-right: 1px solid white">
&nbsp;300 - 305&nbsp;

我目前使用的代码成功地从每个单元格中提取数据并将其粘贴到活动工作表的 A 列中:

Sub GetData()

    Dim URL As String
    Dim IE As InternetExplorer
    Dim HTMLdoc As HTMLDocument
    Dim TDelements As IHTMLElementCollection
    Dim TDelement As HTMLTableCell
    Dim r As Long

    'For login use
    Dim LoginForm As HTMLFormElement
    Dim UserNameInputBox As HTMLInputElement
    Dim PasswordInputBox As HTMLInputElement

    URL = "https://www.whatever.com"

    Set IE = New InternetExplorer

    With IE
        .navigate URL
        .Visible = True

        'Wait for page to load
        While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend

        Set HTMLdoc = .document

            'Enter login info
            Set LoginForm = HTMLdoc.forms(0)

            'Username
            Set UserNameInputBox = LoginForm.elements("username")
            UserNameInputBox.Value = "username"

            'Password
            Set PasswordInputBox = LoginForm.elements("password")
            PasswordInputBox.Value = "password"

            'Get the form input button and click it

            Set SignInButton = LoginForm.elements("doLogin")
            SignInButton.Click

            'Wait for the new page to load

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

        'Auto-navigate to start page, so we need to navigate once more

        .navigate URL

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

        End With


    'Specify how to recognize data to extract
    Set TDelements = HTMLdoc.getElementById("spt_inner_row_2").getElementsByTagName("TD")


    r = 0

    For Each TDelement In TDelements

        ActiveSheet.Range("A1").Offset(r, 0).Value = TDelement.innerText

        r = r + 1

    Next

End Sub

我真正需要的是仅提取 HTML 表格行中的最后一个(最右边)单元格。有什么建议吗?

【问题讨论】:

标签: vba excel html-parsing


【解决方案1】:

IHTMLElementCollection 有一个 length 属性和一个 item 属性。 item 属性可以采用数字索引,但从零开始,因此最后一个条目位于 length - 1

Dim TDelements As IHTMLElementCollection

Set TDelements = HTMLdoc.getElementById("spt_inner_row_2").getElementsByTagName("TD")

With TDelements
    MsgBox .Item(.Length - 1).InnerText
End With

【讨论】:

    猜你喜欢
    • 2013-01-10
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    相关资源
    最近更新 更多