>

요소의 내용이 아닌 "요소 이름"을 긁어 내려고했습니다. 'data-promo-name'으로 가져 오려고하면 결과는 아래 코드에서 '1 개월 첫 임대료 $1'이어야합니다.

class="ps-properties-property__units__prices col-4 col-md-3" data-promo-id="132" data-promo-name="$1 first month rent">

웹 사이트 : https://www.publicstorage.com/self-storage-mi-ann-arbor/1760?sp=1760|1|Ann%20Arbor|42.28083|-83.74303|0|1 | 1

PFB 코드 :

Sub Element_Name()
Dim ie As New InternetExplorer, ws As Worksheet
Dim element As IHTMLElement
Set ws = ThisWorkbook.Worksheets("Unit Data")
With ie
    .Visible = True
    .Navigate2 "https://www.publicstorage.com/self-storage-mi-ann-arbor/1760?sp=1760|1|Ann%20Arbor|42.28083|-83.74303|0|1|1"
    While .Busy Or .readyState < 4: DoEvents: Wend
    Dim listings As Object, listing As Object, headers(), results(), r As Long, c As Long, item As Object
    headers = Array("Width", "Length", "Hight/Space Type", "promo", "Reguler Price", "Online Price", "Listing Active", "features", "features1", "features2", "features3", "features4", "features5", "features6")
    Set listings = .document.getElementsByClassName("row ps-properties-property__units__row ps-properties-property__units__row__desktop")
    ReDim results(1 To listings.Length, 1 To UBound(headers) + 1)
    For Each listing In listings
        r = r + 1
        On Error Resume Next
        results(r, 1) = listing.getElementsByClassName("ps-properties-property__units__header")(0).innerText 'Size
        'results(r, 4) = listing.getElementsByClassName(Need a code here) 'Promo
        results(r, 5) = listing.getElementsByClassName("ps-properties-property__units__prices__old-price")(0).innerText 'Sizet 'Reguler Price
        results(r, 6) = listing.getElementsByClassName("ps-properties-property__units__prices__price")(0).innerText 'Online Price
        results(r, 7) = listing.getElementsByTagName("ps-properties-property__units__prices col-1 col-md-3")(0).innerText 'Listing Active
        results(r, 8) = listing.getElementsByClassName("ps-properties-property__units__feature")(0).innerText 'Features
        On Error GoTo 0
    Next
    ws.Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
    ws.Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
    .Quit
End With

  • 답변 # 1

    노드를 분리 한 다음 getAttribute 함수를 사용해야합니다

    node.getAttribute("data-promo-name")
    
    

    html이 다르게 나타납니다.

    한 줄을 사용한 예는

    ie.document.querySelector(".ps-properties-property__units__prices.col-4.col-md-3").getAttribute("data-promo-name")
    
    

    전체 코드 :

    Option Explicit
    Public Sub ElementName()
        Dim ie As New InternetExplorer, ws As Worksheet
        Dim element As IHTMLElement
        Set ws = ThisWorkbook.Worksheets("Unit Data")
        With ie
            .Visible = True
            .Navigate2 "https://www.publicstorage.com/self-storage-mi-ann-arbor/1760?sp=1760|1|Ann%20Arbor|42.28083|-83.74303|0|1|1"
            While .Busy Or .readyState < 4: DoEvents: Wend
            Dim listings As Object, listing As Object, headers(), results(), r As Long, c As Long, item As Object
            headers = Array("Width", "Length", "Hight/Space Type", "promo", "Reguler Price", "Online Price", "Listing Active", "features", "features1", "features2", "features3", "features4", "features5", "features6")
            Set listings = .document.getElementsByClassName("row ps-properties-property__units__row ")
            Dim html2 As HTMLDocument
            Set html2 = New HTMLDocument
            ReDim results(1 To listings.Length, 1 To UBound(headers) + 1)
            For Each listing In listings
                r = r + 1
                On Error Resume Next
                results(r, 1) = listing.getElementsByClassName("ps-properties-property__units__header")(0).innerText 'Size
                html2.body.innerHTML = listing.outerHTML
                results(r, 4) = html2.querySelector(".ps-properties-property__units__prices").getAttribute("data-promo-name")
                'results(r, 4) = listing.getElementsByClassName(Need a code here) 'Promo
                results(r, 5) = listing.getElementsByClassName("ps-properties-property__units__prices__old-price")(0).innerText 'Sizet 'Reguler Price
                results(r, 6) = listing.getElementsByClassName("ps-properties-property__units__prices__price")(0).innerText 'Online Price
                results(r, 7) = listing.getElementsByTagName("ps-properties-property__units__prices col-1 col-md-3")(0).innerText 'Listing Active
                results(r, 8) = listing.getElementsByClassName("ps-properties-property__units__feature")(0).innerText 'Features
                On Error GoTo 0
                html2.body.innerHTML = vbNullString
            Next
            ws.Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
            ws.Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
            .Quit
        End With
    End Sub
    
    

  • 답변 # 2

    findElement(By.xpath("//div[@class='ps-properties-property__units__prices col-4 col-md-3']")).getAttribute("data-promo-name");
    
    

  • 이전 python - "syntaxerror - lexer를 빌드 할 수 없습니다"는 무엇을 의미합니까?
  • 다음 go - mongodb에서 배열에 객체를 추가하는 방법