excel vba网络剪刀有一天的工作一半
我有一个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)
- 示例列表的更多事实 - https://www.realtor.ca/real-estate/24551273/604-freeman-crescent-kingston
- 没有必要的登录必要
- 在单个列表,html类和id之间, 。
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)
- Example listing - https://www.realtor.ca/real-estate/24551273/604-freeman-crescent-kingston
- no login necessary
- while specifics vary between individual listings, html classes and ids remain constant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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