从 Frame VBA Excel IE8 自动化内部循环遍历 HTML 文档中的 TD 元素

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

Loop Through TD Element in HTML document from inside Frame VBA Excel IE8 Automation

htmlexcelexcel-vbahtml-tableframevba

提问by Burgertron

I am trying to capture Table data inside a particular webpage Frame using Excel VBA.

我正在尝试使用 Excel VBA 在特定网页框架内捕获表数据。

Unfortunately the website is non-public, so I cannot give access.

不幸的是,该网站是非公开的,因此我无法提供访问权限。

Here is a snippet from the END of HTML (showing the LAST cell data) I extracted from Firefox Debugger from the Table I need.

这是我从 Firefox Debugger 从我需要的表格中提取的 HTML 结尾的片段(显示最后一个单元格数据)。

<td class="ewrcc s35 b-17"><div class="ewrcv-nw">ExampleValue</div></td>
</tr></tbody></table>

I have edited my thread post as @Tim Williams kindly pointed my in the right direction to first set a reference to the Frame inside the webpage (Frame 12)

我编辑了我的帖子,@Tim Williams 亲切地指出我的正确方向,首先设置对网页内框架的引用(第 12 帧)

After setting a reference, I have found all of the Table data using:

设置参考后,我使用以下方法找到了所有表数据:

Set HTMLDoc = ie.document.frames(12).document
Set HTMLTD = HTMLDoc.getElementsByTagName("TD")

For xTD = 0 to HTMLTD.length - 1  

j = Len(HTMLDoc.getElementsByTagName("TD")(xTD).innertext)   

    If j > 0 and j < 50 then

    '''Code here

    debug.print HTMLDoc.getElementsByTagName("TD")(xTD).className
    debug.print HTMLDoc.getElementsByTagName("TD")(xTD).Innertext

    End If

Next xTD

I have produced a spreadsheet and can capture the data in a somewhat cumbersome way by using the Innertext from a static known string ("Oct") and then cycling a count through the number of columns known for each table I am retreiving (2 in this case)

我已经制作了一个电子表格,并且可以通过使用静态已知字符串(“Oct”)中的 Innertext 以一种有点麻烦的方式捕获数据,然后通过我正在检索的每个表的已知列数循环计数(在此为 2案件)

Usually I loop through the Row/Cell .length, but I have found when looping through the TD elements they do not have ROW/CELL objects from the method I am using. I would much prefer this way as it appeared faster, and I could automate the task without prompting a variable to trigger where the table populates.

通常我循环遍历 Row/Cell .length,但我发现在循环遍历 TD 元素时,它们没有来自我使用的方法的 ROW/CELL 对象。我更喜欢这种方式,因为它看起来更快,而且我可以自动执行任务,而不会提示变量在表填充的位置触发。

My question is now: Is there anything I am overlooking? Eg. Can I access the table through the Reference Frame 12 and cycle through Children / Parent etc.? I would really like to reference the Table Object to cycle through the rows & cells if possible

我现在的问题是:有什么我忽略的吗?例如。我可以通过 Reference Frame 12 访问表格并循环访问 Children / Parent 等吗?如果可能的话,我真的很想引用表对象来循环遍历行和单元格

Here is a larger snippet of the last row of a Table, the values I need to extract are labelled "DATA VALUE 1-21":

这是表格最后一行的较大片段,我需要提取的值标记为“DATA VALUE 1-21”:

</tr>
<tr class="r46" style="height:15pt;">
<td class="ewrcc s4 b-33"></td>
<td class="ewrcc s23 b-21"><div class="ewrcv-nwl">DATA VALUE 1/21</div>
</td><td class="ewrcc s24 b-21"></td>
<td class="ewrcc s24 b-21"></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 2/21</div></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 3/21</div></td>
<td colspan="3" class="ewrcc s23 b-35">
<div class="ewrcv-nwl">DATA VALUE 4/21</div></td>
<td ewrcolumnindex="9" class="ewrcc s24 b-21">
<div class="ewrcv-nw">DATA VALUE 5/21</div></td>
<td class="ewrcc s23 b-24"><div class="ewrcv-nwl">DATA VALUE 6/21</div></td>
<td class="ewrcc s23 b-21"></td>
<td class="ewrcc s23 b-21"></td><td colspan="2" class="ewrcc s23 b-35">
<div class="ewrcv-nwl">DATA VALUE 7/21</div></td>
<td ewrcolumnindex="15" class="ewrcc s34 b-21">
<div class="ewrcv-nw">DATA VALUE 8/21</div></td>
<td class="ewrcc s25 b-24"><div class="ewrcv-nw">DATA VALUE 9/21</div></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 10/21</div></td>
<td class="ewrcc s24 b-24"><div class="ewrcv-nw">DATA VALUE 11/21</div></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 12/21</div></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 13/21</div></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 14/21</div></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 15/21</div></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 16/21</div></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 17/21</div></td>
<td class="ewrcc s24 b-24"><div class="ewrcv-nw">DATA VALUE 18/21</div></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 19/21</div></td>
<td class="ewrcc s33 b-24"><div class="ewrcv-nw">DATA VALUE 20/21</div>
</td><td class="ewrcc s33 b-35">
<div class="ewrcv-nw">DATA VALUE21/21</div>/td>
</tr>

回答by Tim Williams

How many tables are there in that frame?

那个框架中有多少张桌子?

Try something like:

尝试类似:

Set HTMLDoc = ie.document.frames(12).document
Set tbls = HTMLDoc.getElementsByTagName("table")

For x = 0 to tbls.length - 1  
    Set tbl = tbls(x)
    debug.print "Table# " & (x+1), "rows=" & tbl.Rows.length
Next x