如何在标记内访问HTML标记的innerText

时间:2012-09-29 16:36:34

标签: html excel-vba html-parsing excel-2003 vba

我想从包含此内容的网页中获取一些文字。我希望用href =“#spec_Brand”获得这条信息。

<td class="table_spec">
    <dl>
        <dt class="table_spec_title">
            <a class="href_icon href_icon_help table_spec_titleimg" title="Which manufacturer is producing the product?" href="#spec_Brand">
                <span>Brand</span>
            </a>
            <span class="table_spec_titletext">Brand</span>
        </dt>
        <dd class="table_spec_definition">
            Producer of the product?
        </dd>
    </dl>
</td>

我正在尝试使用:

Set TDelementsA = HTMLdoc.getElementsByTagName("TD")
    While r < TDelementsA.Length
      If TDelementsA.className = "table_spec" Then
         Sheet4.Range("A1").Offset(r, c).Value = TDelement.innerText
    End If

但它给了我: 牌 产品的制作人?

取代

spec_Brand

有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

这是你在尝试什么? (注意我将上述html存储在Sheet1的单元格A1中进行测试)。我也在使用IE的Late Binding

Option Explicit

Sub Sample()
    Dim ie As Object
    Dim links As Variant, lnk As Variant

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.navigate "About: Blank"

    ie.document.body.innerhtml = Sheets("Sheet1").Range("A1").Value

    Set links = ie.document.getElementsByTagName("a")

    For Each lnk In links
        If lnk.classname = "href_icon href_icon_help table_spec_titleimg" Then
            Debug.Print lnk.innertext
            Exit For
        End If
    Next
End Sub

<强> SCREENSHOT

enter image description here