How to access innerText of HTML tag inside a <TD> tag

北城以北 提交于 2019-12-13 07:04:10

问题


I would like to get some text from a web page containing this. I want to have the piece of information with the 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>

I'm trying to use:

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

but it gives me: Brand Producer of the product?

In stead of

spec_Brand

Can someone help me?


回答1:


Is this what you are trying? (Note: I stored the above html in Cell A1 of Sheet1 for testing). Also I am using Late Binding with IE

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



来源:https://stackoverflow.com/questions/12654430/how-to-access-innertext-of-html-tag-inside-a-td-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!