markusreyes2907
New Member
- Joined
- Jul 14, 2020
- Messages
- 34
- Office Version
- 2013
- Platform
- Windows
I am just learning to use the Internet Explorer controls in VBA to webscrape and I'm wondering how to create conditional statements to pull the HTML that I need. Below in my code pulls the description and price from a website that I'm using to practice. It works mostly, however a few of the prices are in a different HTML format (most are in a <span> but these few are in a <div>). I'll provide examples of the code below.
And here is the VBA code I am using.
HTML:
<span class="red pspecial-inline">
<span class="glyphicon glyphicon-electricity"></span>
<span class="text-currency">€</span>
417,81*</span>
//and
<div class="pprice">
<span class="text-currency">€</span>
187,59*</div>
And here is the VBA code I am using.
VBA Code:
Sub webpracscrape()
Dim ele As Object
Dim desc As String, price As String
Dim y As Integer
ie.Visible = True
ie.Navigate "http://www.mindfactory.de/Hardware/Monitore+(TFT).html"
Do While ie.Busy = True Or ie.ReadyState <> 4: DoEvents: Loop
y = 1
For Each ele In ie.Document.getElementsByClassName("pcontent")
desc = ele.getElementsByClassName("pname")(0).innerText
price = ele.getElementsByClassName("pprice")(0).getElementsByTagName("span")(0).innerText
With Range("A" & y)
.Value = desc
.Columns.AutoFit
End With
Range("B" & y).Value = price
y = y + 1
Next
End Sub
Last edited: