• 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.

VBA - web scraping from page 2

Hi All - I am using the following code to scrap product titles from this website for page 1 & 2. It does the job for page 1 and list all the product titles in column A for page 1 but loop doesn't work for page 2. Will appreciate the help?

Code:
Sub DaraNext()
    Dim ie As SHDocVw.InternetExplorer
    Dim idoc As MSHTML.HTMLDocument
    Dim doc_ele As MSHTML.IHTMLElement
    Dim doc_eles As MSHTML.IHTMLElementCollection

    Dim rownum As Variant
    Dim Prodtitle As String
    
    Set ie = New SHDocVw.InternetExplorer
    ie.Visible = True
    ie.navigate "https://www.daraz.lk/catalog/?from=input&q=sarees"
    
       Do While ie.readyState <> READYSTATE_COMPLETE
       Application.StatusBar = "Loading"
    Loop
    Set idoc = ie.document
            
For k = 1 To 2

    On Error Resume Next
      
    For Each doc_ele In doc_eles
    If doc_ele.className = ("ant-pagination-item-link") Then
        doc_ele.Click
        End If
        
        Next doc_ele
          
        Application.Wait Now + TimeValue("00:00:10")

' product title

    Set doc_eles = idoc.getElementsByClassName("c16H9d")
    rownum = 1
    
    For Each doc_ele In doc_eles
        If doc_ele.className = "c16H9d" Then
        Prodtitle = doc_ele.innerText
        ActiveSheet.Cells(rownum, 1).Value = Prodtitle
        rownum = rownum + 1
        End If
    Next doc_ele
    
    
    
    
        Next k
                  
End Sub
 
I have updated the code below was missing one line:



Code:
Sub DaraNext()
    Dim ie As SHDocVw.InternetExplorer
    Dim idoc As MSHTML.HTMLDocument
    Dim doc_ele As MSHTML.IHTMLElement
    Dim doc_eles As MSHTML.IHTMLElementCollection

    Dim rownum As Variant
    Dim Prodtitle As String
    
    Set ie = New SHDocVw.InternetExplorer
    ie.Visible = True
    ie.navigate "https://www.daraz.lk/catalog/?from=input&q=sarees"
    
       Do While ie.readyState <> READYSTATE_COMPLETE
       Application.StatusBar = "Loading"
    Loop
    Set idoc = ie.document
            
For k = 1 To 2

    On Error Resume Next
    Set doc_eles = idoc.getElementsByTagName("a") 
    For Each doc_ele In doc_eles
    If doc_ele.className = ("ant-pagination-item-link") Then
        doc_ele.Click
        End If
        
        Next doc_ele
          
        Application.Wait Now + TimeValue("00:00:10")

' product title

    Set doc_eles = idoc.getElementsByClassName("c16H9d")
    rownum = 1
    
    For Each doc_ele In doc_eles
        If doc_ele.className = "c16H9d" Then
        Prodtitle = doc_ele.innerText
        ActiveSheet.Cells(rownum, 1).Value = Prodtitle
        rownum = rownum + 1
        End If
    Next doc_ele
    
    
    
    
        Next k
                  
End Sub
 

Attachments

  • Book3.xlsm
    17.2 KB · Views: 10
Hi !​
Again, what do you observe / notice just comparing the addresses between webpages #2 & 3 ? (found by a child in few seconds …)
 
Back
Top