ferocious12
Member
Hi Guys - I am using following code for web scraping. It provide me with Image URL in column A & price in Column B. There are either two prices (original & discounted) or original price only for some products. The class name for both original and discounted price is the same. The code bring both prices and I require only discounted price in column B.
HTML for discounted price:
<span id="product-price-64534" data-price-amount="399" data-price-type="finalPrice" class="price-wrapper "><span class="price">₹ 399</span></span>
HTML for original price:
<span id="old-price-64534" data-price-amount="798" data-price-type="oldPrice" class="price-wrapper "><span class="price">₹ 798</span></span>
I have attached a sample workbook and reflected desired outcome in column C highlighted in yellow.
Any help?
HTML for discounted price:
<span id="product-price-64534" data-price-amount="399" data-price-type="finalPrice" class="price-wrapper "><span class="price">₹ 399</span></span>
HTML for original price:
<span id="old-price-64534" data-price-amount="798" data-price-type="oldPrice" class="price-wrapper "><span class="price">₹ 798</span></span>
I have attached a sample workbook and reflected desired outcome in column C highlighted in yellow.
Any help?
Code:
Sub LB1()
Dim ie As SHDocVw.InternetExplorer
Dim idoc As MSHTML.HTMLDocument
Dim doc_ele As MSHTML.IHTMLElement
Dim doc_eles As MSHTML.IHTMLElementCollection
Dim startoftitle As Integer, endoftitle As Integer, rownum As Variant
Dim Prodtitle As String, Price As String, OrigPrice As String, ImgUrl As String
Set ie = New SHDocVw.InternetExplorer
ie.Visible = True
ie.navigate "https://www.leemboodi.com/catalogsearch/result/?q=1037s50"
Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "Loading"
Loop
Set idoc = ie.document
'image URL
Set doc_eles3 = idoc.getElementsByClassName("product-image-photo default_image ")
Z = 1
For Each doc_ele3 In doc_eles3
If doc_ele3.className = "product-image-photo default_image " Then
'doc_ele.Click
ImgUrl = doc_ele3.src
ActiveSheet.Cells(Z, 1).Value = ImgUrl
Z = Z + 1
End If
Next doc_ele3
' price
Set doc_eles = idoc.getElementsByClassName("price")
rownum = 1
For Each doc_ele In doc_eles
If doc_ele.className = ("price") Then
Prodtitle = doc_ele.innerText
ActiveSheet.Cells(rownum, 2).Value = Prodtitle
rownum = rownum + 1
End If
Next doc_ele
ActiveSheet.Columns(1).EntireColumn.AutoFit
ie.Quit
End Sub