getElementsById-对象相关错误Excel VBA
我正在研究一个项目,以从Realtor.com导入特定的房地产列表信息到Excel。该代码应访问Cell A1中清单的链接,并返回该物业在C1中构建的一年。我可以使用getElementsbyClassName进行某些搜索,但是使用(#)。innertext变得不切实际。当我使用getElementsById时,我会收到错误91或错误424-这两者都涉及丢失的对象。我是VBA和编码的新手,因此,任何建议和一点耐心将不胜感激!
我现在正在使用的代码:
Sub GetYearBuilt()
Dim request As Object
Dim response As String
Dim html As New HTMLDocument
Dim website As String
Dim YearBuilt As Variant
'cell A1 contains a url to search
website = Range("A1")
Set request = CreateObject("MSXML2.XMLHTTP")
request.Open "Get", website, False
request.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
request.send
response = StrConv(request.responseBody, vbUnicode)
html.body.innerHTML = response
'this line either causes error 91 - object variable or with block variable not set, or error 424 - object required
YearBuilt = html.getElementById("propertyDetailsSectionContentSubCon_BuiltIn")(0).innerText
'return address to cell C1
Range("C1") = YearBuilt
End Sub
HTML代码正在尝试提取:
<div class="propertyDetailsSectionContentSubCon" id="propertyDetailsSectionContentSubCon_BuiltIn">
<div class="propertyDetailsSectionContentLabel">Built in</div>
<div class="propertyDetailsSectionContentValue">1973</div>
</div>
虽然我可以使用类“ propertyDetailssectionContentValue”类,但整个网站中的该类都太多了。
I'm working on a project to import specific real-estate listing information from realtor.com to excel. This code should visit a link to a listing in cell A1, and return the year the property was built in c1. I am able to use the getElementsByClassName for certain searches, but with different listings, using (#).innerText becomes impractical. When I use getElementsById, I get either error 91 or error 424 - both of which refer to a missing object. I'm quite new to VBA and coding in general, so any advice and a little patience would be greatly appreciated!
Code I'm using now:
Sub GetYearBuilt()
Dim request As Object
Dim response As String
Dim html As New HTMLDocument
Dim website As String
Dim YearBuilt As Variant
'cell A1 contains a url to search
website = Range("A1")
Set request = CreateObject("MSXML2.XMLHTTP")
request.Open "Get", website, False
request.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
request.send
response = StrConv(request.responseBody, vbUnicode)
html.body.innerHTML = response
'this line either causes error 91 - object variable or with block variable not set, or error 424 - object required
YearBuilt = html.getElementById("propertyDetailsSectionContentSubCon_BuiltIn")(0).innerText
'return address to cell C1
Range("C1") = YearBuilt
End Sub
html code I'm trying to extract from:
<div class="propertyDetailsSectionContentSubCon" id="propertyDetailsSectionContentSubCon_BuiltIn">
<div class="propertyDetailsSectionContentLabel">Built in</div>
<div class="propertyDetailsSectionContentValue">1973</div>
</div>
While I could use the class "propertyDetailsSectionContentValue", there are far too many items with this class throughout the website.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论