Excel VBA:从在线HTML表获取内容 [英] Excel VBA: get content from online HTML table

查看:713
本文介绍了Excel VBA:从在线HTML表获取内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以向我显示VBA代码的一部分,这将从此示例在线HTML表格中获取文本hello?第一个节点将被他的ID(id =something)找到。

  ... 
< table ID = 东西 >
< tr>
< td>< TABLE>< TR>< TD>< / TD>< / TR>< TR>< TD>< / TD>< / TR> /表>< / TD>< TD>< / TD>
< / tr>
< tr>
< td>< / td>< td>< / td>< td> hello< / td>
< / tr>
...

我认为这将像孩子 - > sibling-> child-> sibling-> sibling-> child ,但我不知道确切的方式。



编辑
更新的代码标签是CAPITALS。所以如果我使用 getElemenetsById(something)。getElemenetsByTagName('tr')它只得到两个tr标签来收集,或者四个(标签是更深的孩子)?

解决方案

如果您确实搜索了一个答案,您可能希望下次扩大您的范围。有很多问题和答案处理DOM的东西和VBA。



在HTMLElement上使用getElementById而不是HTMLDocument



虽然问题(和答案)不是完全你想要的,显示如何创建可以使用的东西。



您需要使用 getElementById() getElemenetsByTagName()以检索您想要的hello



例如:文档。 getElementById(something)。getElementsByTagName(tr)(1).getElementsByTagName(td)(2).innerText




  • 获取元素something

  • 在something中获取所有tr标签(特别是索引1中的标签)

  • 在返回的tr标签中,获取所有td标签(特别是索引2中的标签)

  • 获取以前结果的innerText



    • 这些对象s使用0的数组,所以第一个项目是item(0)。



      更新



      document.getElementById()将返回一个(单数)IHTMLElement(将包括其所有子项)或无/如果它不存在。



      document.getElementsByTagName()将返回集合 IHTMLElement(再次,每个元素将包括其所有的孩子)。 (如果没有,则为空集合)



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



      document.getElementsByTagName(tr)(0)将返回第一个(单数)IHTMLElement从集合。 (注意结尾处的索引?)



      VBA中没有(我可以找到)InternetExplorer对象的兄弟功能,所以你必须使用子索引手动执行。



      使用DOM函数干净的方法。它比只是看一条链Element.Children(0).children(1).children(2)更清楚,因为你不知道索引是什么意思,没有手动查找。


      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").

      ...
      <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.

      EDIT updated 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)?

      解决方案

      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.

      Use getElementById on HTMLElement instead of HTMLDocument

      While the question (and answers) aren't exactly what 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"

      eg: 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

      These objects use a 0 based array so the first item is 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.getElementsByTagName() will return a collection of IHTMLElement (again, each element will include all of its children). (or an empty collection if none exist)

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

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

      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.

      Using the DOM Functions is the 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.

      这篇关于Excel VBA:从在线HTML表获取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆