I'm trying to grab som data from a webpage with Excel VBA.
The HTML code is:
<div id="PriceLabel" class="skuPrice formRow">
<span class="skuPriceWrp">
<strong class="price red" id="skuPriceLabel" style="font-size: 20px;">
<span class="VAT">eks mva </span><br>
<span itemprop="price" id="SkuPriceUpdate">kr
<span itemprop="priceCurrency" content="NOK">151,20</span>
</span>
</strong>
<span> /
<span class="UOM" telephone="no">RL
</span>
</span>
</span>
<span class="skuUOMWrp">
<span id="showEachPrice" class="clear"></span>
</span>
<div class="formRow clear" id="divSkuSavePrice">
<span id="divSkuWhenYouBuy">
<span class="whenYouSave"></span>
</span>
</div>
</div>
What i want to grab is the value 151,20
, located on the first "span".
I've tried: .document.getElementById("skuPriceLabel").getElementsByTagName("span")(0).innerText
This gives the value "Eks mva" sometimes, and runtime error 424 (object required) at other times.
I've tried using .document.getElementById("skuPriceLabel").getElementsByTagName("span")(1).innerText
and
.document.getElementById("skuPriceLabel").getElementsByTagName("span")(2).innerText
as well, but those give the 424 runtime error.
Can somebody please give show me how to read the html code so i can see the logic and know what to look for and write the next time?
My full code is
Sub get_data_2()
'Source for this code is:
'http://stackoverflow.com/questions/26613043/get-data-out-of-a-webpage-with-vba
Dim ie As Object
Dim sht As Worksheet
Dim SKU As String
Dim RowCount As Long
Set sht = Sheet8
Set ie = CreateObject("InternetExplorer.application")
RowCount = 1
'This just gives the columns a titel i row numer 1.
sht.Range("a" & RowCount) = "SKU" 'Column A is populated with SKU's to be looked up.
sht.Range("n" & RowCount) = "Price" 'Column N will be given the price of the SKU.
With ie
.Visible = True
.navigate "http://www.staples.no/"
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
Do
RowCount = RowCount + 1
SKU = sht.Range("a" & RowCount).Value
With ie 'fill in the searchbox and submit.
ie.document.all("searchKeywords").Value = SKU 'we can use 491215 as a SKU for this example.
ie.document.forms("searchForm").submit
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
'write the price to column N
sht.Range("n" & RowCount).Value = ie.document.getElementById("skuPriceLabel").getElementsByTagName("span")(1).getElementsByTagName("span")(0).innerText
End With
Loop While sht.Range("a" & RowCount + 1).Value <> "" 'Loop as long as column A has a SKU (till end of list).
End With
Set ie = Nothing
End Sub
I would say you have 2 options:
1. DOM
.document.getElementById("skuPriceLabel").getElementsByTagName("span")(1).getElementsByTagName("span")(0).innerText
2. Regex
Use regex:
content=""NOK"">(.*?)<
with this function