Excel VBA:从在线 HTML 表格中获取内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16141394/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:45:10  来源:igfitidea点击:

Excel VBA: get content from online HTML table

htmlexcelvbadom

提问by koubin

can anybody pleas show me part of VBA code, which will get text "hello" from this example online HTML table? first node will be found by his ID (id="something").

任何人都可以向我展示 VBA 代码的一部分,该代码将从这个示例在线 HTML 表中获取文本“你好”吗?第一个节点将通过他的 ID (id="something") 找到。

...
<table id="something">
  <tr>
    <td><TABLE><TR><TD></TD></TR><TR><TD></TD></TR></TABLE></td><td></td>
  </tr>
  <tr>
    <td></td><td></td><td>hello</td>
  </tr>
...

i think it will be something like child->sibling->child->sibling->sibling->child, but I don't know the exact way.

我认为它会像child->sibling->child->sibling->sibling->child,但我不知道确切的方式。

EDITupdated code tags are CAPITALS. so if I use getElemenetsById("something").getElemenetsByTagName('tr')it get only two tr tags to collection, or four (with tags which are deeper children)?

编辑更新的代码标签是大写的。因此,如果我使用getElemenetsById("something").getElemenetsByTagName('tr')它,只能收集两个 tr 标签,还是四个(带有更深的孩子的标签)?

回答by NickSlash

If you did search for an answer, you might want to broaden your scope next time. There are plenty of questions and answers that deal with DOM stuff and VBA.

如果您确实在寻找答案,那么下次您可能想扩大范围。有很多关于 DOM 和 VBA 的问题和答案。

Use getElementById on HTMLElement instead of HTMLDocument

在 HTMLElement 上使用 getElementById 而不是 HTMLDocument

While the question (and answers) aren't exactlywhat you want, it will show you how to create something you can work with.

虽然问题(和答案)并不完全是您想要的,但它会向您展示如何创建您可以使用的东西。

You'll need to use a mixture of getElementById()and getElemenetsByTagName()to retrieve your desired "hello"

你需要使用的混合物getElementById(),并getElemenetsByTagName()取回您的期望“你好”

eg: Document.getElementById("something").getElementsByTagName("tr")(1).getElementsByTagName("td")(2).innerText

例如: Document.getElementById("something").getElementsByTagName("tr")(1).getElementsByTagName("td")(2).innerText

  • Get the element "something"
  • Inside "something" get all "tr" tags (specifically the one at index 1)
  • Inside the returned tr tag get all "td" tags (specifically the one at index 2)
  • Get the innerText of the previous result
  • 获取元素“某物”
  • 在“某物”中获取所有“tr”标签(特别是索引 1 处的标签)
  • 在返回的 tr 标签中获取所有“td”标签(特别是索引 2 处的标签)
  • 获取上一个结果的innerText

These objects use a 0 based array so the first item is item(0).

这些对象使用基于 0 的数组,因此第一项是 item(0)。

Update

更新

document.getElementById()will return an(singular) IHTMLElement (which will include all of its children) or nothing/null if it does not exist.

document.getElementById()将返回一个(单数)IHTMLElement(它将包括它的所有子元素)或如果它不存在则没有/null。

document.getElementsByTagName()will return a collectionof IHTMLElement (again, each element will include all of its children). (or an empty collection if none exist)

document.getElementsByTagName()将返回IHTMLElement的集合(同样,每个元素将包括其所有子元素)。(如果不存在,则为空集合)

document.getElementsByTagName("tr")this will return all tr elements inside the "document" element.

document.getElementsByTagName("tr")这将返回“document”元素内的所有 tr 元素。

document.getElementsByTagName("tr")(0)will return the first(singular) IHTMLElement from the collection. (note the index at the end?)

document.getElementsByTagName("tr")(0)将从集合中返回第一个(单数)IHTMLElement。(注意最后的索引?)

There is no (that i could find) "sibling" feature of the InternetExplorer object in VBA, so you'd have to do it manually using the child index.

VBA 中没有(我能找到的)InternetExplorer 对象的“同级”功能,因此您必须使用子索引手动执行此操作。

Using the DOM Functions isthe clean way to do it. Its much clearer than just looking at a chain "Element.Children(0).children(1).children(2)" as you've no idea what the index means without manually looking it up.

使用 DOM 函数一种干净的方式。它比只查看链“Element.Children(0).children(1).children(2)”要清晰得多,因为如果不手动查找索引,您将不知道它的含义。

回答by Rrgg

I looked all over for the answer to this question, too. I finally found the solution by talking to a coworker which was actually through recording a macro.

我也到处找这个问题的答案。我终于通过与同事交谈找到了解决方案,这实际上是通过录制宏。

I know, you all think you are above this, but it is actually the best way. See the full post here: http://automatic-office.com/?p=344In short, you want to record the macro and go to data --> from web and navigate to your website and select the table you want.

我知道,你们都认为自己在这之上,但这实际上是最好的方式。请参阅此处的完整帖子:http: //automatic-office.com/?p=344简而言之,您想要记录宏并转到数据 --> 从网络并导航到您的网站并选择您想要的表格。

I have used the above solutions "get element by id" type stuff in the past, and it is great for a few elements, but if you want a whole table, and you aren't super experienced, just record a macro. don't tell your friends and then reformat it to look like your own work so no one knows you used the macro tool ;)

我过去使用过上述解决方案“通过 id 获取元素”类型的东西,它对少数元素非常有用,但是如果你想要一个完整的表格,而且你不是非常有经验,只需记录一个宏。不要告诉你的朋友,然后重新格式化它看起来像你自己的作品,所以没有人知道你使用了宏工具;)