excel - VBA 错误 438 对象在尝试从网站抓取网页时不支持此属性或方法
问题描述
我在尝试使用 VBA 从网站获取元素时遇到问题,我在 StackOverflow 中搜索了这个问题,但我尝试过的任何答案都解决了我的问题。
我想获取元素Strong中的文本,但是这个元素进入了一个Div。
Sub StatusInvest()
Dim html As HTMLDocument
Set html = New HTMLDocument
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://statusinvest.com.br/acoes/eua/aapl", False
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" 'to deal with potential caching
.send
html.body.innerHTML = .responseText
End With
Application.ScreenUpdating = False
Set v = html.getElementsByClass("info special w-100 w-md-33 w-lg-20")(0).Value = "00000"
Set test = valor.getElementsByTagName("Strong").innerText
Range("B1").Value = teste
Application.ScreenUpdating = True
End Sub
这是我要获取的元素: HTML 示例
解决方案
您的方法名称错误 - 应该是
getElementsByClassName
.getElementsByTagName
并且getElementsByClassName
两者都返回元素的集合,无论是否有一个或多个,因此当您尝试获取strong
元素时必须参考索引 0。innertext
property 返回一个字符串,所以你不应该使用Set
statement 而只是 assigntest
。Sub StatusInvest() Dim html As HTMLDocument Dim valor As Object Dim test As String Set html = New HTMLDocument With CreateObject("MSXML2.XMLHTTP") .Open "GET", "https://statusinvest.com.br/acoes/eua/aapl", False .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" 'to deal with potential caching .send html.body.innerHTML = .responseText End With Application.ScreenUpdating = False Set valor = html.getElementsByClassName("special")(0) test = valor.getElementsByTagName("Strong")(0).innerText Range("B1").Value = test Application.ScreenUpdating = True End Sub
推荐阅读
- python - 在 Pandas Dataframe 中按天连接字符串列表
- ios - 检查用户连续使用应用程序的天数
- r - R - 从字符列中的值创建动态指标列
- time-complexity - 从 0 到 n 的所有斐波那契数的时间复杂度
- python - 为什么 scipy.spatial.ckdtree 运行速度比 scipy.spatial.kdtree 慢
- r - 是什么使负数成为lubridate?
- sql - 在 Active Record 中查询数据库之前删除空格在 Rails C 中有效,但在控制器中无效
- javascript - 在加载数据时如何在 react-native FlatList 中呈现 isLoading 元素
- asp.net - 为什么我在 Azure blob 存储中的文本文件中的文本在下载时格式不正确?
- vmware - 我们应该如何决定 VMWare 所需的虚拟处理器和 RAM 的数量