• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How to compact output sourced from a table within a cell?

shahin

Active Member
How can i get all the data derived from a table out of a webpage? I've tried myself to accomplish the task and I'm very close to the solution. Any help to reach me there will be highly appreciated.

The macro I've tried with:
Code:
Sub Get_Table()
    Dim HTML As HTMLDocument, post As Object
    Dim elem As Object, overview$, URL$
    URL = "https://www.strollay.com/orange-silk-saree-137116"

    With CreateObject("InternetExplorer.Application")
        .Visible = False
        .navigate URL
        While .Busy = True Or .readyState < 4: DoEvents: Wend
        Set HTML = .document
      
        For Each post In HTML.getElementsByClassName("data-table")(0).Rows
            For Each elem In post.Cells
                overview = overview & IIf(overview = "", "", " : ") & elem.innerText
            Next elem
        Next post
        [A1] = overview
    End With
End Sub

The output I'm getting:
Code:
Item Code : GST18944-902 : Fabric : Silk : Blouse Fabric : Silk : Blouse Type : Un-Stitched : Saree Length : 6.00 Mtrs : Blouse Length : 0.80 Mtrs : Work : Embroidered : Delivery Time : It takes 3-6 Working Days once Shipped.

The output I wish to get:
Code:
Item Code : GST18944-902 , Fabric : Silk , Blouse Fabric : Silk , Blouse Type : Un-Stitched , Saree Length : 6.00 Mtrs , Blouse Length : 0.80 Mtrs , Work : Embroidered , Delivery Time : It takes 3-6 Working Days once Shipped.

For more clarity: I wish to kick out this ":" sign in every alternate position as in, [2,4,6,8, so on] and put "," in place. Thanks.
 
Last edited:
Yep, as Marc has stated. You should build concatenation logic from get go, to generate the result you want.

You could use counter to keep track or use some other logic to check.
 
@Marc L, Because I would like to get the items and it's result displayed like so and the website doesn't contain ":" sign between them.
 
Really, it's just using what Marc and I have shown you in the past.

Here's demo. In your case, you'd use some variable to hold count of concatenation done to use as condition.
Code:
Sub Demo()
Dim ar
Dim overview As String
ar = Array("Item Code", "GST18944-902", "Fabric", "Silk", "Blouse Fabric", "Silk", "Blouse Type ", "Un-Stitched", "Saree Length", "6.00 Mtrs", "Blouse Length", "0.80 Mtrs", "Work", "Embroidered", "Delivery Time", "It takes 3-6 Working Days once Shipped.")

For i = 0 To UBound(ar)
    Select Case (i + 1) Mod 2
        Case Is = 1
            overview = IIf(Len(overview) = 0, ar(i), overview & ", " & ar(i))
        Case Is = 0
            overview = overview & " : " & ar(i)
    End Select
Next
Debug.Print overview
End Sub

Alternately, you adjust concatenation logic slightly and check for right most character.

Code:
Sub Demo()
Dim ar
Dim overview As String
ar = Array("Item Code", "GST18944-902", "Fabric", "Silk", "Blouse Fabric", "Silk", "Blouse Type ", "Un-Stitched", "Saree Length", "6.00 Mtrs", "Blouse Length", "0.80 Mtrs", "Work", "Embroidered", "Delivery Time", "It takes 3-6 Working Days once Shipped.")

For i = 0 To UBound(ar)
    If Len(overview) = 0 Then
        overview = ar(i) & " :"
    ElseIf Right(overview, 1) = ":" Then
        overview = overview & " " & ar(i) & ","
    Else
        overview = overview & " " & ar(i) & " :"
    End If
Next
overview = Left(overview, Len(overview) - 1)
Debug.Print overview
End Sub
 
No way!!!! So damn perfect. Thanks a zillion sir. Btw, what is "Mod" which you have used in your first demo?
 
@Marc L, Because I would like to get the items and it's result displayed like so and the website doesn't contain ":" sign between them.
Right …

But again your code shows a misobservation of the web
document & data as it just needs a loop only :​
Code:
Sub Demo1()
         Dim S$(), R%
    With CreateObject("InternetExplorer.Application")
             .Navigate "https://www.strollay.com/orange-silk-saree-137116"
       While .Busy Or .ReadyState < 4:  DoEvents:  Wend
        With .Document.all("product-attribute-specs-table").Rows
          If .Length Then
                 ReDim S(.Length - 1)
            For R = 0 To .Length - 1
                S(R) = Replace(.Item(R).innerText, vbCrLf & vbCrLf, " : ")
            Next
                [A1].Value = Join(S, " , ")
          End If
        End With
             .Quit
    End With
End Sub
Do you like it ? So thanks to click on bottom right Like !
 

Another way at level of beginner observing eyes :​
Code:
Sub Demo2()
    Dim S$, R%
    With CreateObject("WinHttp.WinHttpRequest.5.1")
            .Open "GET", "https://www.strollay.com/orange-silk-saree-137116", False
            .setRequestHeader "DNT", "1"
            .send
        S = .responseText
    End With
    With CreateObject("htmlfile")
             .body.innerHTML = S
        With .all("product-attribute-specs-table").Cells
          If .Length > 1 Then
               S = .Item(0).innerText & " : " & .Item(1).innerText
           For R = 2 To .Length - 2 Step 2
               S = S & " , " & .Item(R).innerText & " : " & .Item(R + 1).innerText
           Next
               [A1].Value = S
          End If
        End With
    End With
End Sub
You may Like it !
 
Thanks Marc L, for your awesome demo. Your second approach does it the way I expected to do. This time I could understand that ".all()" supports only ID not CLASS. Thanks again.
 
« Second approach » is just a variation of first one but with same logic !
Just see object and variables used. Compare with your code.
Concatenation is at very beginner level …

Next time, stop to start your code by a bad copy/paste
but just start to well observe the web document …
Next time if I see any not necessary variables like your HTML & post(s)
or any superfluous loop, I will directly move to next thread !

And no, all object supports not only Id …
 
Much Obliged. I myself also get vexed when I see the name of my variables. However, I should stop practicing that. Btw, Is there any thread where it has been described what type of names I should choose to name variables?
 

Nothing to do with their names (no matter)
but just about not necessary loop (as yet warned in your previous threads)
and not necessary variables ! Just directly work with the right object …​
 
Back
Top