excel vba网络剪刀有一天的工作一半

发布于 2025-02-08 06:51:45 字数 2143 浏览 0 评论 0原文

我有一个VBA程序,用于从在线房地产清单中刮下特定数据。我将在下面添加代码。它搜索HTML代码以返回元素,包括地址,建造年份,功能等...

Sub GetAddress()

Dim request As Object
Dim response As String
Dim html As New HTMLDocument
Dim website As String
Dim address As Variant

'would prefer to reference a cell that already has a url, e.g. A1
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

'the class associated with the inormation I'm searching for is unsetH1, and there is only one instance of this class in the html
address = html.getElementsByClassName("unsetH1")(0).innerText

'return address to cell B1
Range("B1") = address


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 etiher causes error 91 - object variable or with block variable not set, or error 424 - object required
yearbuilt = html.getElementById("propertyDetailsSectionContentSubCon_BuiltIn").getElementsByClassName("propertyDetailsSectionContentValue")(0).innerText

'return address to cell C1
Range("C1") = yearbuilt

这只是代码的一部分。其余的重复仅通过更改class名称和ID来找到其他元素。

我的问题是代码有时有效,然后无需更改任何内容,然后再次对其进行测试,它返回一个错误(运行时91-对象变量或块未设置),

  • 通常在打开Excel程序并立即运行它之后工作,但是,如果我在成功尝试后再次运行它,第二个将不起作用
  • ,则说明错误发生在getElementby ...线上,但是我不明白该线如何能够完美地工作,但是失败了下一个。

我是编程的新手,所以也许我缺少一个简单/明显的解决方案。任何建议都将不胜感激!

update

有关网站(realor.ca)

I have a VBA program used to scrape specific data from online real estate listings. I will add the code below. It searches the HTML code to return elements including Address, Year built, features, etc...

Sub GetAddress()

Dim request As Object
Dim response As String
Dim html As New HTMLDocument
Dim website As String
Dim address As Variant

'would prefer to reference a cell that already has a url, e.g. A1
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

'the class associated with the inormation I'm searching for is unsetH1, and there is only one instance of this class in the html
address = html.getElementsByClassName("unsetH1")(0).innerText

'return address to cell B1
Range("B1") = address


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 etiher causes error 91 - object variable or with block variable not set, or error 424 - object required
yearbuilt = html.getElementById("propertyDetailsSectionContentSubCon_BuiltIn").getElementsByClassName("propertyDetailsSectionContentValue")(0).innerText

'return address to cell C1
Range("C1") = yearbuilt

This is only a portion of the code. The rest of it repeats to find other elements just by changing class name and Id.

My problem is that the code sometimes works, and then without changing anything and then testing it again, it returns an error (runtime 91 - object variable or with block not set)

clues

  • it usually works after opening the excel program and immediately running it, but if I run it again after a successful attempt, the second one will not work
  • it says that the error occurs in the getElementby... lines, but I don't understand how that line can work perfectly one time, but then fail the next.

I am fairly new to programming, so perhaps there is a simple/obvious solution that I am missing. Any suggestions would be much appreciated!

Update

More facts about the website (realor.ca)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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