getElementsById-对象相关错误Excel VBA

发布于 2025-02-07 03:50:51 字数 1383 浏览 1 评论 0原文

我正在研究一个项目,以从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文