Excel VBA:使用 HTML DOM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15881790/
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
Excel VBA: using HTML DOM
提问by koubin
I need to know what all can I do with HTML over Excel VBA. for example I know that I can find element by id
ie.document.getElementByID()
.
I will work with HTML table which doesn't have elements with id, so that it will look like child->child->sibling->child.....
i think.
我需要知道我可以通过 Excel VBA 使用 HTML 做什么。例如我知道我可以通过 id 找到元素
ie.document.getElementByID()
。我将使用没有带 id 的元素的 HTML 表,这样它看起来就像child->child->sibling->child.....
我想的那样。
Can anybody pleas show me part of code, which will get text "hello" from this example table? first node will be found by his ID.
任何人都可以向我展示部分代码,从这个示例表中得到文本“你好”吗?第一个节点将通过他的 ID 找到。
...
<table id="something">
<tr>
<td></td><td></td>
</tr>
<tr>
<td></td><td>hello</td>
</tr>
...
回答by c2n
I'm looking at this type of thing at the moment...
我现在正在看这种类型的东西......
I believe the something like the below should do it:
我相信像下面这样的事情应该这样做:
ie.getelementbyid("something").getelementsbytagname("TD")(3).innertext
How it works:
这个怎么运作:
It searches the HTML doc for the element where the ID is equal to "something" (will take first iteration if more than 1 but you can loop through many). It will then get take the table data tag and go to the iteration (3) where the text is (0 would equal the first TD).
它在 HTML 文档中搜索 ID 等于“某物”的元素(如果超过 1,将进行第一次迭代,但您可以循环遍历多个)。然后它将获取表数据标记并转到文本所在的迭代 (3)(0 将等于第一个 TD)。
Let us know if this works.
让我们知道这是否有效。
回答by Brian
Many HTML documents have names in generalized tables as opposed to ID's
许多 HTML 文档在通用表中都有名称,而不是 ID
I commonly use some form of what is shown below;
我通常使用如下所示的某种形式;
Set HTML = IE.Document
Set SomethingID = HTML.GetElementByID("something").Children
For Each TR in SomethingID
If TR.TagName = "td" Then
If TR.Name = "myname" Then
'code in here if you are looking for a name to the element
ElseIf TR.Value = "myValue" Then
'Code in here if you are looking for a specific value
ElseIf TR.innerText = "mytext" Then
'more code in here if you are looking for a specific inner text
End If
End If
Next TR