Webscrape Conditionals

markusreyes2907

New Member
Joined
Jul 14, 2020
Messages
34
Office Version
  1. 2013
Platform
  1. 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.


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:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Forum statistics

Threads
1,214,929
Messages
6,122,315
Members
449,081
Latest member
tanurai

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top