问题
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