从vba中的speicific类获取href值

时间:2016-02-19 19:26:52

标签: excel vba internet-explorer

我想从以下代码中获取href链接:

<div class="border-content">
        <div class="main-address">

            <h2 class="address">
                <a href="/Propiedades/Detalles/7717095--Departamento-tipo-casa-de-1-Dormitorio-en-Venta-en-Capital-Federal?ViewNameResult=VistaResultados" title="Marcos paz 2500. Villa Devoto - Capital Federal">Marcos paz 2500<span></span></a>
            </h2>

我尝试使用getelementsbytagname(&#34; a&#34;)但我不知道如何为特定班级#34;地址&#34;做到这一点。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

谢谢Kilian,这是我处理一切的方式。相当复杂,但它确实有效,尽管我需要大量的嵌套循环:

Sub Propiedades()

'to refer to the running copy of Internet Explorer
Dim ie As InternetExplorer
'to refer to the HTML document returned
Dim html As HTMLDocument
'open Internet Explorer in memory, and go to website
Set ie = New InternetExplorer
ie.Visible = False
ie.Navigate "http://www.argenprop.com/Departamentos-tipo-casa-Venta-Almagro-Belgrano-Capital-Federal/piQ86000KpsQ115000KmQ2KrbQ1KpQ1KprQ2KpaQ135Kaf_816Kaf_100000001KvnQVistaResultadosKaf_500000001Kaf_801KvncQVistaGrillaKaf_800000002Kaf_800000005Kaf_800000010Kaf_800000041Kaf_800000011Kaf_800000020Kaf_800000030Kaf_800000035Kaf_800000039Kaf_900000001Kaf_900000002Kaf_900000006Kaf_900000008Kaf_900000009Kaf_900000007Kaf_900000010Kaf_900000033Kaf_900000034Kaf_900000036Kaf_900000038Kaf_900000037Kaf_900000035Kaf_900000039Kaf_900000041Kaf_900000042Kaf_900000043"
'Wait until IE is done loading page
Do While ie.ReadyState <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to go to argenprop ..."
DoEvents
Loop
'show text of HTML document returned
Set html = ie.Document
'close down IE and reset status bar
Set ie = Nothing
Application.StatusBar = ""
'clear old data out and put titles in
Sheets(2).Select
Cells.ClearContents
'put heading across the top of row 3
Range("A3").Value = "Direccion"
Range("B3").Value = "Mts cuadrados"
Range("C3").Value = "Antiguedad"
Range("D3").Value = "Precio"
Range("E3").Value = "Dormitorios"
Range("F3").Value = "Descripcion"
Range("G3").Value = "Link"


Dim PropertyList As IHTMLElement
Dim Properties As IHTMLElementCollection
Dim Property As IHTMLElement
Dim RowNumber As Long
Dim PropertyFields As IHTMLElementCollection
Dim PropertyField As IHTMLElement
Dim PropertyFieldLinks As IHTMLElementCollection

Dim caracteristicasfields As IHTMLElementCollection
Dim caract As IHTMLElement
Dim caracteristicas As IHTMLElementCollection
Dim caractfield As IHTMLElement

Set PropertyList = html.getElementById("resultadoBusqueda")

Set Properties = PropertyList.Children

RowNumber = 4

For Each Property In Properties

   If Property.className = "box-avisos-listado clearfix" Then

    Set PropertiesFields = Property.all
        For Each PropertyField In PropertiesFields
            Fede = PropertyField.className
            If PropertyField.className Like "avisoitem*" Then

                Set caracteristicas = PropertyField.Children

                For Each caract In caracteristicas
                    f = caract.className
                    If f = "border-content" Then
                        Set caracteristicasfields = caract.all

                        For Each caractfield In caracteristicasfields

                            test1 = caractfield.className
                            u = caractfield.innerText
                            If caractfield.className <> "" Then
                                Select Case caractfield.className

                                Case Is = "address"
                                    Cells(RowNumber, "A") = caractfield.innerText
                                    marray = Split(caractfield.outerHTML, Chr(34))
                                   Cells(RowNumber, "G") = "www.argenprop.com" & marray(5)
                                Case Is = "list-price"
                                    Cells(RowNumber, "D") = caractfield.innerText
                                Case Is = "subtitle"
                                    Cells(RowNumber, "F") = caractfield.innerText 'descripcion

                                'Case is ="datoscomunes"
                                    'Set myelements = caractfield.all

                                Case Is = "datocomun-valor-abbr"

                                    Select Case counter
                                    Case Is = 0
                                        Cells(RowNumber, "B") = caractfield.innerText 'square mts
                                        counter = counter + 1
                                    Case Is = 1
                                        Cells(RowNumber, "E") = caractfield.innerText 'DORMITORIOS
                                        counter = counter + 1
                                    Case Is = 2
                                        Cells(RowNumber, "C") = caractfield.innerText ' antiguedad
                                        counter = 0 ' reset counter
                                        Set caracteristicasfields = Nothing
                                        Exit For 'salgo del loop en caractfield
                                    End Select 'cierro el select del counter

                                End Select 'cierro el select de caractfield.classname
                            End If ' cierro If caractfield.className <> "" Then
                        Next caractfield

                    End If ' cierro el border content
                    If caract = "border-content" Then Exit For 'salgo del loop dentro de aviso item (caract)

                Next caract
            RowNumber = RowNumber + 1
            End If ' If PropertyField.className Like "avisoitem*"
        Next PropertyField   'para ir al siguiente aviso

   End If


Next Property

Set html = Nothing

MsgBox "done!"
End Sub