从网页中提取数据时出错

Getting error while extracting the data from webpage

提问人:HSHO 提问时间:8/27/2023 更新时间:8/27/2023 访问量:30

问:

我制定了以下代码来从提供的网页中检索数据: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=link&2010988919=variant&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
Excel VBA 网页抓取

评论


答:

3赞 JohnM 8/27/2023 #1

自从您的代码上次成功运行以来,该网站似乎已经重组。尝试将相关行替换为

beds = html.getElementsByClassName("statsValue")(1).innerText
baths = html.getElementsByClassName("statsValue")(2).innerText
sqFt = html.getElementsByClassName("stat-block sqft-section")(0).getElementsByClassName("statsValue")(0).innerText
price = html.getElementsByClassName("stat-block beds-section")(0).getElementsByClassName("statsValue")(0).innerText
est = html.getElementsByClassName("stat-block beds-section")(0).getElementsByClassName("est-monthly-payment")(0).innerText
address = html.getElementsByClassName("street-address")(0).innerText
cityStateZip = html.getElementsByClassName("dp-subtext bp-cityStateZip")(0).innerText

...虽然这现在有效,但自然不能保证它会工作多长时间(如果网站再次重组)。

如果您有兴趣,请在此处阅读更多内容 W3Schools这里 MS Docs.

评论

0赞 HSHO 8/28/2023
我抓住你了!非常感谢