Home > other >  VBA scraping get inner text from class
VBA scraping get inner text from class

Time:07-24

I have a code that captures data from here:

https://www.cigarsofcuba.co.uk/shop/cuban-cigars/bolivar-cigars/bolivar-belgravia-uk-regional-2015-cigars-box-of-10/

The code below works quite well and captures necessary data in Excel.

Dim posts As Object, post As Object, r&

With CreateObject("InternetExplorer.Application")
    .Visible = False
    .navigate "https://www.cigarsofcuba.co.uk/shop/cuban-cigars/bolivar-cigars/bolivar-belgravia-uk-regional-2015-cigars-box-of-10/"
    While .Busy = True Or .readyState < 4: DoEvents: Wend


    Do: Set posts = .Document.getElementsByClassName("product type-product"): DoEvents: Loop Until posts.Length > 0

    For Each post In posts
        With post.getElementsByClassName("woocommerce-loop-product__title")
            If .Length Then
                Cells(counter, 1) = .Item(0).innerText
                Cells(counter, 3) = "Lloyd"
                Cells(counter, 4) = Now()
           End If
        End With
        With post.getElementsByClassName("woocommerce-Price-amount amount")
            If .Length Then Cells(counter, 2) = CDbl(.Item(0).innerText)
        End With
        
        With post.getElementsByTagName("a")
            If .Length Then Cells(counter, 8) = .Item(0).href
        End With
        
        counter = counter   1
    Next post
    .Quit
End With

I am now trying to add a code to capture if the stock is "out of stock" like this:

        With post.getElementsByClassName("stock out-of-stock")
            If .Length Then Cells(counter, 6) = .Item(0).innerText
        End With

but this fails to identify that its out of stock and capture "out of stock" in the relevant column/row

the relevant html part is here:

<div >
    <p >Out of stock</p>
    <section >

CodePudding user response:

The elements you are looping over, selected by the following code, are the related products listed at the bottom of the page.

Set posts = .document.getElementsByClassName("product type-product")

This list of related items does not hold info about stock levels so your proposed test, If .Length, is False and the code moves on to the End With.

The test within this proposed section that is:

With post.getElementsByClassName("stock out-of-stock")
    If .Length Then Cells(counter, 6) = .Item(0).innerText
End With

I am unsure why you visit this page, which does hold stock level info for the main product, and only go on to scrape the related items info off this same page without visiting the linked pages to scrape stock info from them.

You need to visit those links you wrote out to column H in order to scrape stock level info, and potentially also scrape info about the main product which you currently you do not appear to retrieve.

  • Related