Я пытаюсь получить информацию о ценах обычной (126,37 €) и сниженной (101,10 €) информации с этого сайта.
Упрощенный код HTML выглядит следующим образом:
<div class="vw-productFeatures ">
<ul class="feature-list -price-container">
<li class="feature -price">
<span class="value">126,37</span>
</li>
</ul>
<ul class="feature-list vw-productVoucher">
<li class="voucher-information">Mit Code
<span class="voucher-reduced-price">101,10</span>
</li>
</ul>
</div>
Итак, я в основном go шаг за шагом (div класс -> ul class -> li class -> span class) и получаю innerText в конце.
I Я могу получить обычную цену, однако spanclass.innerText
по сниженной цене возвращается пустым.
Это код, с которым я работаю:
Function getHTMLelemFromCol(HTMLColIn As MSHTML.IHTMLElementCollection, tagNameIn As String, classNameIn As String) As MSHTML.IHTMLElement
Dim HTMLitem As MSHTML.IHTMLElement
For Each HTMLitem In HTMLColIn
If (HTMLitem.tagName = tagNameIn) Then
If (HTMLitem.className = classNameIn) Then
Set getHTMLelemFromCol = HTMLitem
Exit For
End If
End If
Next HTMLitem
End Function
Function getPrice(webSite As String, divClass As String, ulClass As String, liClass As String, spanClass As String) As String
Dim XMLPage As New msxml2.XMLHTTP60
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim HTMLitem As MSHTML.IHTMLElement
Dim HTMLObjCol As MSHTML.IHTMLElementCollection
XMLPage.Open "GET", webSite, False
XMLPage.send
HTMLDoc.body.innerHTML = XMLPage.responseText
Set HTMLObjCol = HTMLDoc.getElementsByClassName(divClass)
Set HTMLitem = getHTMLelemFromCol(HTMLObjCol, "DIV", divClass) ' Find the div class we are interested in first
Set HTMLitem = getHTMLelemFromCol(HTMLitem.Children, "UL", ulClass) ' Find the ul class we are interested in
Set HTMLitem = getHTMLelemFromCol(HTMLitem.Children, "LI", liClass) ' Find the li class we are interested in
Set HTMLitem = getHTMLelemFromCol(HTMLitem.Children, "SPAN", spanClass) ' Find the span class we are interested in
getPrice = HTMLitem.innerText
End Function
Sub Run()
Dim webSite As String, divClass As String, ulClass As String, liClass As String, spanClass As String, regularPrice As String, reducedPrice As String
webSite = "https://www.rakuten.de/produkt/msi-b450-tomahawk-max-atx-mainboard-4x-ddr4-max-64gb-1x-dvi-d-1x-hdmi-14-1x-usb-c-31-2843843890"
divClass = "vw-productFeatures "
' Get the regular price
ulClass = "feature-list -price-container"
liClass = "feature -price"
spanClass = "value"
regularPrice = getPrice(webSite, divClass, ulClass, liClass, spanClass)
' Get the reduced price
ulClass = "feature-list vw-productVoucher -hide"
liClass = "voucher-information"
spanClass = "voucher-reduced-price"
reducedPrice = getPrice(webSite, divClass, ulClass, liClass, spanClass)
Debug.Print "Regular price: " & regularPrice
Debug.Print "Reduced price: " & reducedPrice
End Sub
Вывод, который я получаю:
Regular price: 126,37
Reduced price:
Отладчик показывает, что он может найти правильный класс span, но у него нет атрибута (включая innerText), который содержит информацию о цене.
Как получить сокращенный информация о цене?