通过内部框架VBA Excel IE8自动化的HTML文档中的TD元素循环 [英] Loop Through TD Element in HTML document from inside Frame VBA Excel IE8 Automation
问题描述
我正在尝试使用Excel VBA捕获特定网页内的表格数据。
不幸的是,该网站是非公开的,所以我无法访问。
这是从我从需要的表中从Firefox Debugger中提取的HTML的END(显示LAST单元格数据)的代码段。
< td class =ewrcc s35 b-17>< div class =ewrcv-nw> ExampleValue< / div>< / TD>
< / tr>< / tbody>< / table>
我已经编辑我的帖子,作为@Tim威廉姆斯请指出我的方向,首先设置一个参考网页内的框架(框架12)
设置参考后,我已经找到了所有的Table数据:
设置HTMLDoc = ie.document.frames(12).document
设置HTMLTD = HTMLDoc.getElementsByTagName(TD)
对于xTD = 0到HTMLTD.length - 1
j = Len(HTMLDoc.getElementsByTagName(TD)(xTD).innertext)
如果j> 0和j < 50然后
'''这里代码
debug.print HTMLDoc.getElementsByTagName(TD)(xTD).className
debug.print HTMLDoc.getElementsByTagName( TD)(xTD).Innertext
如果
下一个xTD
我已经制作了一个电子表格,并且可以通过使用静态已知字符串(Oct)中的Innertext,以一种有点麻烦的方式捕获数据,然后循环计数通过每个已知的列数表我正在回报(在这种情况下为2)
通常我循环遍历行/单元格长度,但是我发现循环访问TD元素时ROW / CELL对象从我使用的方法。我更喜欢这种方式,因为它出现得更快,我可以自动执行任务,而不会提示一个变量来触发表填充的位置。
我现在的问题是:有没有任何我忽视的东西例如。可以通过参考框架12访问表格,并循环通过儿童/家长等?我真的很想参考Table Object来遍历行&如果可能,单元格
下面是表格最后一行的更大的代码段,我需要提取的值被标记为数据值1-21:
< / tr>
< tr class =r46style =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 =3class =ewrcc s23 b-35>
< div class =ewrcv-nwl> DATA VALUE 4/21< / div>< / td>
< td ewrcolumnindex =9class =ewrcc s24 b-21>
< div class =ewrcv-nw> DATA VALUE 5/21< / div>< / td>
< td class =ewrcc s23 b-24>< div class =ewrcv-nwl>数据值6/21< / div>< / td>
< td class =ewrcc s23 b-21>< / td>
< td class =ewrcc s23 b-21>< / td>< td colspan =2class =ewrcc s23 b-35>
< div class =ewrcv-nwl> DATA VALUE 7/21< / div>< / td>
< td ewrcolumnindex =15class =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>数据值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>数据值12/21< / div>< / td>
< td class =ewrcc s33 b-24>< div class =ewrcv-nw>数据值13/21< / div>< / td>
< td class =ewrcc s33 b-24>< div class =ewrcv-nw>数据值14/21< / div>< / td>
< td class =ewrcc s33 b-24>< div class =ewrcv-nw>数据值15/21< / div>< / td>
< td class =ewrcc s33 b-24>< div class =ewrcv-nw>数据值16/21< / div>< / td>
< td class =ewrcc s33 b-24>< div class =ewrcv-nw>数据值17/21< / div>< / td>
< td class =ewrcc s24 b-24>< div class =ewrcv-nw>数据值18/21< / div>< / td>
< td class =ewrcc s33 b-24>< div class =ewrcv-nw>数据值19/21< / div>< / td>
< td class =ewrcc s33 b-24>< div class =ewrcv-nw>数据值20/21< / div>
< / td>< td class =ewrcc s33 b-35>
< div class =ewrcv-nw> DATA VALUE21 / 21< / div> / td>
< / tr>
该框架中有多少人? p>
尝试一下:
设置HTMLDoc = ie.document.frames( 12).document
设置tbls = HTMLDoc.getElementsByTagName(table)
对于x = 0到tbls.length - 1
设置tbl = tbls(x)
debug.print表#& (x + 1),rows =& tbl.Rows.length
下一个x
I am trying to capture Table data inside a particular webpage Frame using 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.
<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)
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)
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.
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
Here is a larger snippet of the last row of a Table, the values I need to extract are labelled "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>
How mant ables 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
这篇关于通过内部框架VBA Excel IE8自动化的HTML文档中的TD元素循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!