【问题标题】:Get href value from speicific class in vba从vba中的特定类获取href值
【发布时间】:2016-06-01 12:05:33
【问题描述】:

我想从下面的代码中获取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("a") 但我不知道如何为特定的类“地址”执行此操作。有什么想法吗?

【问题讨论】:

  • 你搜索过这个吗?如果您将您的标题放入Google,则会弹出许多结果。甚至是SO 上的一个。这些都没有帮助吗?请让我们知道您尝试过什么、搜索过什么以及您的努力是否奏效。
  • 我在网页中看到的所有代码都是“a”,所以我无法使用它。我最终通过创建一个将其拆分为 chr(34) 的数组来使用 outerHTML 属性,然后将其取出。不是最好的方法,但它有效
  • 你是怎么做到的?使用纯字符串操作?常用表达?你试过一些 XPath 吗?
  • 在这里试试这个:stackoverflow.com/questions/23476502/… Cheers

标签: excel vba internet-explorer


【解决方案1】:

感谢 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

【讨论】:

    猜你喜欢
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    相关资源
    最近更新 更多