18910140161

HTML-尝试只提取一段内部文本-堆栈溢出

顺晟科技

2022-10-18 13:20:07

112

Option Explicit
Sub VBAWebscraping2()


    Dim IEObject As Object
    
    Set IEObject = New InternetExplorer
    
    IEObject.Visible = True
    
    IEObject.navigate url:="https://streeteasy.com/building/" & Cells(2, 4).Value
        
    Do While IEObject.Busy = True Or IEObject.readyState <> READYSTATE_COMPLETE
    
        Application.Wait Now + TimeValue("00:00:01")
        
    Loop
    
    Dim IEDocument As HTMLDocument
    Set IEDocument = IEObject.document
    
    'GRAB by classname'
    
    Dim IEElements As IHTMLElementCollection
    Dim IEElement As IHTMLElement
    
    Set IEElements = IEDocument.getElementsByClassName("details")
    
    For Each IEElement In IEElements
    If IEElement.innerText = "price" Then
        Debug.Print (IEElement.innerText)
    End If
    Exit For
    Next
    
    
        
    'Dim lastRow As Long
    'lastRow = Range("A" & Rows.count).End(xlUp).row
    
End Sub

它转到所需的位置,但当它试图提取价格时,它要么不在调试菜单中打印,要么抓取太多。试图找出一种更精确的方法来只抓住价格,很多教程都提到了ID标签,但在网站上并不多。

这就是我https://streeteasy.com/building/the-cambridge-500-east-85-street-new_york/15l?card=1

要找的网站。

还尝试检索房间、浴室和邻居

的数量

顺晟科技:

您可以使用类

价格

IEDocument.querySelector(".price").innerText
房间

也一样

IEDocument.querySelector(".first_detail_cell").innerText

IEDocument.querySelector("[class='detail_cell ']").innerText

洗澡

IEDocument.querySelector("last_detail_cell").innerText

看看这些类是如何命名的,如果这些项目中的任何一个缺失或顺序不同,您在不同页面上的里程几乎肯定会有所不同。然后,您需要循环通过为类.detail_cell选择而返回的元素列表,并测试.innerText是否存在文本";room";,";bed"或";洗澡,并相应地分配。

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航