我制定了以下代码来从提供的网页检索数据: https: //www.redfin.com/NV/Fallon/630-Serpa-Pl-89406/unit-Fallon/home/178255574 ?utm_source=android_share&utm_medium=share&utm_nooverride =1&utm_content=链接&2010988919=变体&utm_campaign=homecard_share。
但是,我在以下行遇到“需要对象”错误: sqFt = html.getElementsByClassName("statsLabel")(0).NextSibling.getElementsByClassName("statsValue")(0).innerText。
我希望有人可以提供帮助来纠正此问题,使代码能够成功地从网页中提取相关信息。
我们将非常感谢您的指导。谢谢。
Sub ExtractingRedfin()
Dim url As String
Dim ie As Object
Dim html As Object
Dim Lastrow As Long
Lastrow = Sheet1.Cells(Sheet1.Rows.Count, 1).End(xlUp).Row
' URL of the webpage to scrape
url = Sheet1.Cells(Lastrow, 1).Value
' Create a new Internet Explorer instance
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False ' Set to True if you want to see the browser
' Navigate to the URL
ie.navigate url
' Wait for the browser to finish loading the page
Do While ie.readyState <> 4 Or ie.Busy
DoEvents
Loop
' Get the HTML content of the page
Set html = ie.document
Dim beds As String
Dim baths As String
Dim sqFt As String
Dim price As String
Dim est As String
Dim address As String
Dim cityStateZip As String
beds = html.getElementsByClassName("statsValue")(0).innerText
baths = html.getElementsByClassName("statsValue")(1).innerText
sqFt = html.getElementsByClassName("statsLabel")(0).NextSibling.getElementsByClassName("statsValue")(0).innerText
price = html.getElementsByClassName("info-block price")(0).getElementsByClassName("statsValue")(0).innerText
est = html.getElementsByClassName("info-block price")(0).getElementsByClassName("statsLabel")(0).innerText
address = html.getElementsByClassName("street-address")(0).innerText
cityStateZip = html.getElementsByClassName("citystatezip")(0).innerText
ie.Quit
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet3")
ws.Cells(2, 1).Value = beds
ws.Cells(2, 2).Value = baths
ws.Cells(2, 3).Value = sqFt
ws.Cells(2, 4).Value = price
ws.Cells(2, 5).Value = est
ws.Cells(2, 6).Value = address
ws.Cells(2, 7).Value = cityStateZip
End Sub
自您的代码上次成功运行以来,该网站似乎已经进行了重组。尝试将相关行替换为
...虽然现在才有效,但自然不能保证这会有效多久(如果网站再次重组)。
如果您有兴趣,请在W3Schools和MS Docs阅读更多内容。