Results 1 to 3 of 3

Thread: IE.Document.getElementsByTagName("A")
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Sep 2014
    Posts
    628
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default IE.Document.getElementsByTagName("A")

    So I have some code which pulls URLs from URLs by tagname if it contains the If Instr in the code below.

    I'd like this to focus just on one section of the webpage, Div Class:

    Div Class: product-list-container

    and exclude:

    Div Class: recommender__wrapper

    The code is as follows:

    Code:
    Sub Getalllinks_tescos()
    
    
    Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
    Dim cnt As Long: cnt = 1
    Dim AR() As Variant: AR = Range("A1:A3").Value
    Dim AL() As Variant
    Dim url_name As String
    Dim LR As Long
    
    
    IE.Visible = True
    
    
    For i = LBound(AR) To UBound(AR)
        url_name = AR(i, 1)
    
    
        If url_name = "" Then Exit For
        IE.navigate (url_name)
    
    
        Do
            DoEvents
        Loop Until IE.ReadyState = 4
    
    
        Set AllHyperlinks = IE.Document.getElementsByTagName("A")
    
    
        For Each hyper_link In AllHyperlinks
            If InStr(hyper_link.href, "https://www.tesco.com/groceries/en-GB/products/") Then
                ReDim Preserve AL(1 To cnt)
                AL(cnt) = hyper_link.href
                cnt = cnt + 1
            End If
        Next
     
    Next i
    
    
    IE.Quit
    
    
    Range("B1").Resize(UBound(AL), 1).Value = Application.Transpose(AL)
    
    
    End Sub
    Many thanks in advance.

  2. #2
    Board Regular
    Join Date
    Sep 2014
    Posts
    628
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: IE.Document.getElementsByTagName("A")

    Any ideas thanks?

  3. #3
    MrExcel MVP
    Moderator
    xenou's Avatar
    Join Date
    Mar 2007
    Location
    Clev. OH, USA
    Posts
    16,467
    Post Thanks / Like
    Mentioned
    11 Post(s)
    Tagged
    1 Thread(s)

    Default Re: IE.Document.getElementsByTagName("A")

    Possibly:
    Code:
        Set AllHyperlinks = IE.Document.getElementsByClassName("product-list-container")(0).getElementsByTagName("A")
    resources for web scraping (might help with the basics):
    http://automatetheweb.net/common-vba...eb-automation/
    Last edited by xenou; Aug 26th, 2019 at 08:57 AM.

    Using: Office 2010/Win7 (work) Office 2010/Win7 (home)

    You are rich in proportion to the number of things you can let alone.
    -- Henry David Thoreau

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •