如何访问 <TD> 标签内 HTML 标签的innerText
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12654430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access innerText of HTML tag inside a <TD> tag
提问by user1708574
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".
我想从包含此内容的网页中获取一些文本。我想获得带有 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?
有人能帮我吗?
回答by Siddharth Rout
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
这是你正在尝试的吗?(注意:我将上述 html 存储在 Sheet1 的 Cell A1 中以进行测试)。我也在 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
截屏