获取属性值在VBA [英] Getting attribute values in VBA
问题描述
我需要做一个VBA文件,将读取网页并返回IMG标签的SRC属性的值。我无法使最后一步的工作。你们可以帮我吗?
I need to make a VBA file that will read a webpage and return the value of the SRC attribute of the IMG tag. I wasn't able to make the last step work. Can you guys help me?
<html>
<body>
<img src="image.jpg">
</body>
</html>
===编辑=== 我设法返回属性对象。现在我需要返回其值
===Edit=== I managed to return the attribute object. Now I need to return its value
Option Compare Database
Sub AcessaPagina()
Dim ie As InternetExplorer
Dim test As String
Dim obj As Object
Set ie = New InternetExplorer
ie.Navigate "http://www.google.com.br"
MsgBox ie.Document.getElementsByTagName("img").Item(0).Attributes("src")
ie.Visible = True
End Sub
这就是我目前所面对的。
That's what I have at the moment.
推荐答案
有没有一种方法被称为getElementByTagName - 这就是所谓的的getElementsByTagName (注意在取值,因为它是一家集)
There is no method called "getElementByTagName" -- it's called getElementsByTagName (note the s because it is a collection)
文档对象返回源中所有的img标签的集合。所以,你可以迭代它是这样的:
The Document Object returns a collection of all the img tags in the source. So you can iterate it like this:
Sub AcessaPagina()
Dim ie As Object ' InternetExplorer
Dim images As Object ' MSHTML.IHTMLElementCollection
Dim image As Object ' MSHTML.IHTMLElement
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "http://www.google.com.br"
Set images = GetAllImages(ie)
For Each image In images
Debug.Print image.getAttribute("src")
Next image
End Sub
Function GetAllImages(ie As Object) As Object
Set GetAllImages = ie.document.images
End Function
这篇关于获取属性值在VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!