vba 从 html 获取属性字符串值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13317285/
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
Get attribute string value from html
提问by Yeqing Zhang
I am building a macro to extract data from website using vba. Currently I can easily get value from table content using element syntax like obj.getElementsByTagName("td").innerText
. However, when there are some non-innerText data in some cells, I am getting trouble. It's like this:
我正在构建一个宏来使用vba从网站中提取数据。目前,我可以使用像obj.getElementsByTagName("td").innerText
. 但是,当某些单元格中有一些非innerText 数据时,我遇到了麻烦。就像这样:
<img src="/images/amber_pending.gif" border="0" alt="Pending" title="Pending">
I attempted to extract the attribute value from "title" using syntax I found from others:
我尝试使用从其他人那里找到的语法从“title”中提取属性值:
For Each tbObj In doc.getElementsByClassName("report removeTdBorder")
i = 1
For Each trObj In tbObj.getElementsByTagName("tr")
If i >= 3 Then
j = 1
For Each tdObj In trObj.getElementsByTagName("td")
If j = 1 Then
Set imgObj = tdObj.getElementsByTagName("img")
dataArray(i, j) = imgObj.getAttribute("title")
Debug.Print imgObj.getAttribute("title")
ActiveCell.Offset(0, j) = dataArray(i, j)
ActiveCell.Offset(0, j).WrapText = False
Else
dataArray(i, j) = tdObj.innerText
Debug.Print i & ", " & j & ": " & dataArray(i, j)
ActiveCell.Offset(0, j) = dataArray(i, j)
ActiveCell.Offset(0, j).WrapText = False
End If
j = j + 1
Next tdObj
ActiveCell.Offset(1, 0).Activate
End If
i = i + 1
Next trObj
Next tbObj
But this code goes error every time and it said "Run-time error '438': Object doesn't support this property or method" at the line dataArray(i, j) = imgObj.getAttribute("title")
. Could some one help me?
但是这段代码每次都会出错,它在行中说“运行时错误'438':对象不支持此属性或方法” dataArray(i, j) = imgObj.getAttribute("title")
。有人可以帮助我吗?
采纳答案by Tim Williams
Set imgObj = tdObj.getElementsByTagName("img")
returns a collection of images (even if there's only one to be found), so you can address a specific image using (eg):
返回一组图像(即使只能找到一个),因此您可以使用(例如)来寻址特定图像:
dataArray(i, j) = imgObj(0).getAttribute("title")