vba 从网页/html 表中提取特定 <TD> 的内文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20167953/
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
Pull a specific <TD>'s innertext from webpage/html table
提问by TPJ87
I'm trying to pull the innertext to the <TD>
tag bracketing M118. I was trying to alter this :
我正在尝试将内部文本拉到包含<TD>
M118的标签中。我试图改变这一点:
Range("J" & (ActiveCell.Row)) = IE.Document.getelementbyid("ctl05_BasicdatagridInventoryLocations").getelementbyclass("gridItem").innerText
to find the table and row is there something I can add before innertext to select the <TD>
I'm trying to pull from?
找到表格和行,我可以在innertext 之前添加一些东西来选择<TD>
我想要从中提取的内容吗?
The HTML I'm working with is:
我正在使用的 HTML 是:
<table class="grid" cellspacing="0" cellpadding="1" rules="all" border="1" id="ctl05_BasicdatagridInventoryLocations" style="border-style:Solid;width:100%;border-collapse:collapse;">
<tr class="gridPager" align="left">
<td colspan="7"><span>1</span></td>
</tr>
<tr class="gridHeader">
<td>Quantity</td><td>Class</td><td>UOM</td><td>Warehouse</td><td>Location</td><td>Last Cycle Count</td><td> </td>
</tr>
<tr class="gridItem" onmouseover="cc(this, true);" onmouseout="cc(this, false);">
<td>5</td><td>NEW</td><td>EA</td><td>Rig Warehouse</td><td>M118</td><td>11 Sep 2013</td><td></td>
</tr>
</table>
采纳答案by jacouh
You can use this (Tested in IE10):
您可以使用它(在 IE10 中测试):
Sub sof20167953GetIeWebpage()
Dim objIe As Object
Dim xobj
Set objIe = CreateObject("InternetExplorer.Application")
objIe.Visible = True
'
objIe.navigate "http://www.example.com/MyTable.html"
'
While (objIe.Busy Or objIe.READYSTATE <> 4)
DoEvents
Wend
'
Set xobj = objIe.Document.getElementById("ctl05_BasicdatagridInventoryLocations").getElementsByClassName("gridItem").Item(0)
Set xobj = xobj.getElementsByTagName("td").Item(4)
'
Range("J" & (ActiveCell.Row)) = xobj.innerText
'
Set xobj = Nothing
'
objIe.Quit
Set objIe = Nothing
End Sub
Please notice:
请注意:
- getElementById() gets a single element.
- getElementsByClassName() gets a collection of elements, so use .Item(0) to get first element
- getElementsByTagName() gets a collection of elements, so use .Item(0) to get first element
- getElementById() 获取单个元素。
- getElementsByClassName() 获取元素的集合,因此使用 .Item(0) 获取第一个元素
- getElementsByTagName() 获取元素集合,因此使用 .Item(0) 获取第一个元素