PDA

View Full Version : problem with getelementsby...



Galactico
10-10-2017, 08:21 PM
I'm trying to create a macro to get web data, without success

on the page there are 4 sentences of "stylePrice", I want to obtain them independently (especially with the values (0), (1), (2) and (3)), that's why I want stylePice to be the filter

I would greatly appreciate your help, sorry for my english, not main language.




Sub TESTCK()Dim htmlDeRespuesta As Object
Set htmlDeRespuesta = CreateObject("htmlFile")
With CreateObject("msxml2.xmlhttp")
.Open "Get", "https://www.cardkingdom.com/mtg/ixalan/carnage-tyrant", False
.send
htmlDeRespuesta.body.innerhtml = .responsetext
End With


On Error Resume Next
Range("c4").Value = htmlDeRespuesta.getElementsById("stylePrice").getElementsByTagName("span")(0).innertext
On Error GoTo 0
End Sub

20627

Jan Karel Pieterse
10-11-2017, 12:54 AM
This appears to work:


Sub TESTCK()
Dim htmlDeRespuesta As Object
Dim o As Object
Set htmlDeRespuesta = CreateObject("htmlFile")
With CreateObject("msxml2.xmlhttp")
.Open "Get", "https://www.cardkingdom.com/mtg/ixalan/carnage-tyrant", False
.send
htmlDeRespuesta.body.innerhtml = .responsetext
End With
Set o = htmlDeRespuesta.getElementsByTagName("stylePrice")
For Each o In htmlDeRespuesta.all
If LCase(o.classname) = "styleprice" Then
Range("c4").Value = o.innertext
Exit For
End If
Next
End Sub

snb
10-11-2017, 03:27 AM
Sub M_snb()
With CreateObject("msxml2.xmlhttp")
.Open "Get", "https://www.cardkingdom.com/mtg/ixalan/carnage-tyrant", 0
.send
sn = Filter(Split(Replace(Join(Filter(Split(.responsetext, vbLf), "stylePrice"), ">"), " ", ""), ">"), "$")
End With

Cells(4, 3).Resize(, UBound(sn) + 1) = sn
End Sub

Galactico
10-11-2017, 07:13 AM
Thanks guys, they both worked out perfectly!


little by little I learn more, thanks!

Jan Karel Pieterse
10-11-2017, 08:24 AM
NB: This line in my code is obsolete:

Set o = htmlDeRespuesta.getElementsByTagName("stylePrice")