Excel 从vba中的特定类获取href值
我想从以下代码获取href链接:Excel 从vba中的特定类获取href值,excel,vba,internet-explorer,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
<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”),但我不知道如何为特定的类“address”这样做。有什么想法吗?谢谢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
Sub-Propiedades()
'以引用Internet Explorer的运行副本
Dim ie作为InternetExplorer
'以引用返回的HTML文档
将html设置为HTMLDocument
'在内存中打开Internet Explorer,然后转到网站
Set ie=新的InternetExplorer
可见=假
导航"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_90000006Kaf_90000008Kaf_90000009Kaf_90000007Kaf_900000010Kaf_90000033Kaf_90000034Kaf_90000036Kaf_90000038Kaf_90000037Kaf_90000035Kaf_90000039Kaf_90000041; 90000042Kaf_90000043“
'等待IE完成加载页面
在ie.ReadyState ReadyState\u完成时执行此操作
Application.StatusBar=“正在尝试转到Argentop…”
多芬特
环
'显示返回的HTML文档的文本
设置html=ie.Document
'关闭IE并重置状态栏
设置ie=无
Application.StatusBar=“”
“清除旧数据并输入标题
第(2)页。选择
透明内容物
'将标题放在第3行的顶部
范围(“A3”).Value=“Direccion”
范围(“B3”).Value=“Mts cuadrados”
范围(“C3”).Value=“Antiguedad”
范围(“D3”).Value=“Precio”
范围(“E3”).Value=“宿舍”
范围(“F3”).Value=“描述”
范围(“G3”).Value=“链接”
作为IHTMLElement的Dim PropertyList
将属性设置为IHTMlementCollection
作为ihtmlement的Dim属性
暗行数等于长行数
作为IHTMlementCollection的Dim Property字段
作为IHTMLElement的Dim Property字段
Dim Property字段链接为IHTMlementCollection
作为IHTMlementCollection的Dim Caracteristicas字段
根据IHTMLElement的规定进行调暗
IHTMLElementCollection的暗色特征
将caractfield作为IHTMLElement进行调整
Set PropertyList=html.getElementById(“resultadoBusqueda”)
Set Properties=PropertyList.Children
行数=4
对于属性中的每个属性
如果Property.className=“box avisos listado clearfix”,则
设置PropertiesFields=Property.all
对于PropertiesFields中的每个PropertyField
Fede=PropertyField.className
如果PropertyField.className类似于“avisoitem*”,则
设置caracteristicas=PropertyField.Children
对于caracteristicas中的每个caract
f=caract.className
如果f=“边框内容”,则
设置caracteristicasfields=caract.all
对于CaraCharacteristicasFields中的每个caractfield
test1=caractfield.className
u=caractfield.innerText
如果是caractfield.className“”,则
选择Case caractfield.className
Case Is=“地址”
单元格(行号,“A”)=caractfield.innerText
marray=拆分(caractfield.outerHTML,Chr(34))
单元格(行号,“G”)=“www.argenprop.com”和marray(5)
Case Is=“标价”
单元格(行号,“D”)=caractfield.innerText
Case Is=“subtitle”
单元格(行号,“F”)=caractfield.innerText“description”
'Case is=“datoscomunes”
'设置myelements=caractfield.all
案例为=“datocomun valor abbr”
选择案例计数器
大小写为=0
单元格(行号,“B”)=caractfield.innerText“方形mts
计数器=计数器+1
情况是=1
单元格(行号,“E”)=caractfield.innerText'DORMITORIOS
计数器=计数器+1
情况是=2
单元格(行号,“C”)=caractfield.innerText“antiguedad”
计数器=0'重置计数器
Set-caracteristicasfields=无
在卡拉克特菲尔德的“萨尔戈·德尔·洛普”出口
结束选择“cierro el Select del计数器
结束选择'cierro el Select de caractfield.classname
如果“cierro If caractfield.className”则结束,然后
下卡拉特菲尔德
如果“cierro el边框内容”结束
如果caract=“border content”,则退出“salgo del loop dentro de aviso项目(caract)
下克拉
RowNumber=RowNumber+1
结束If'If PropertyField.className,如“avisoitem*”
Next PropertyField’para ir al-siguiente aviso
如果结束
下一个属性
设置html=Nothing
MsgBox“完成!"
端接头
你搜索过这个吗?如果你把你的标题放进去,会弹出很多结果。甚至有一个结果出现。这些都没有帮助吗?请告诉我们你尝试了什么,搜索了什么,以及你的努力是否奏效。我在哪里看到的代码都是“a”“在网页中,所以我无法使用它。我最终使用了outerHTML属性,创建了一个拆分它的数组