Consulting

Results 1 to 8 of 8

Thread: VBA getElementsByTagName Issues

  1. #1
    VBAX Newbie
    Joined
    Aug 2013
    Posts
    3
    Location

    VBA getElementsByTagName Issues

    Hello,

    I am trying to acquaint myself with VBA's getElementsByTagName, but I am struggling to make it work for me. I use Microsoft Office 2013, and have enabled Microsoft Internet Control and Microsoft HTML Object Library. In the following code, I am trying to grab the birthdate and place it in cell A1. However, this code seems to do nothing. It does not seem to even produce an error.

    Sub Extract_TD_text()

    Dim URL As String
    Dim IE As InternetExplorer
    Dim HTMLdoc As HTMLDocument
    Dim TDelements As IHTMLElementCollection
    Dim TDelement As HTMLTableCell
    Dim r As Long

    'Saved from www vbaexpress com/forum/forumdisplay.php?f=17
    URL = [Wikipedia page for Rihanna (not allowed to paste HTML here)]

    Set IE = New InternetExplorer

    With IE
    .navigate URL
    .Visible = True

    'Wait for page to load
    While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend

    Set HTMLdoc = .document
    End With

    Set TDelements = HTMLdoc.getElementsByTagName("TD")


    r = 0
    For Each TDelement In TDelements
    'Look for required TD elements - this check is specific to VBA Express forum - modify as required
    If TDelement.className = "infobox.vcard.plainlist" Then
    Sheet1.Range("A1").Offset(r, 0).Value = TDelement.innerText
    r = r + 1
    End If
    Next
    IE.Quit

    End Sub
    Thanks for your time.

  2. #2
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    Hi Makeshft,

    Welcome to the forum.

    Getting HTML info can be tricky. You need to delve into the page to understand its structure to get the right information back. In your case the information you really need is in the element after that whose className = "persondata-label". The following should produce a Table with the information that you want.

    Dim URL As String
    Dim IE As InternetExplorer
    Dim HTMLdoc As HTMLDocument
    Dim TDelements As IHTMLElementCollection
    Dim TDelement As HTMLTableCell
    Dim r As Long
    Dim bGetNext As Boolean
    
    'Saved from www vbaexpress com/forum/forumdisplay.php?f=17
    URL = "h**p://en.wikipedia.org/wiki/Rihanna" 'change the ** to tt
    
    Set IE = New InternetExplorer
    
    With IE
        .navigate URL
        .Visible = False
        
        'Wait for page to load
        While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
        
        Set HTMLdoc = .document
    End With
    
    Set TDelements = HTMLdoc.getElementsByTagName("TD")
    
    
    r = 0
    For Each TDelement In TDelements
        If bGetNext Then
            Sheet1.Range("A1").Offset(r - 1, 1).Value = TDelement.innerText
            bGetNext = False
        End If
        'Look for required TD elements - this check is specific to VBA Express forum - modify as required
        'Debug.Print TDelement.className '- useful for inspecting the HTML info
        If TDelement.className = "persondata-label" Then
            Sheet1.Range("A1").Offset(r, 0).Value = TDelement.innerText
            bGetNext = True 'Trigger to get the information from the next TDelement
            r = r + 1
        End If
    Next
    IE.Quit
    
    End Sub


    Hope this helps.
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

  3. #3
    VBAX Newbie
    Joined
    Aug 2013
    Posts
    3
    Location
    Thanks a ton Teeroy!

  4. #4
    VBAX Newbie
    Joined
    Aug 2013
    Posts
    3
    Location
    Sorry, but I'm still having a little trouble figuring out how you knew to look for the class name "persondata-label." I've been fiddling with Firefox inspect tools for a while now, and the only info I can find about the data is the "span-class" info for each piece of info. Is there anyway I could search by span class or other specific information (e.g. divclass)? If this question is a little too broad, is there any material you suggest I read to understand these functions better?

    Thanks again!

  5. #5
    Knowledge Base Approver VBAX Guru
    Joined
    Apr 2012
    Posts
    4,921
    Sub M_snb()
        With New InternetExplorer
            .Navigate "http://en.wikipedia.org/wiki/Rihanna"
             
            Do
              DoEvents
            Loop Until .ReadyState = 4
                 
            For Each it In .Document.getElementsByTagName("TD")
              If it.className = "persondata-label" Then c00 = c00 & "|" & it.innerText & "_" & it.className
            Next
        End With
        
        sn = Split(Mid(c00, 2), "|")
        Cells(1).Resize(UBound(sn) + 1) = Application.Transpose(sn)
    End Sub

  6. #6

    Post getElementsByTagName fail to catch "td" tag elements

    I need help from the smart guys here on the site. The code snippet below don't give any errors and it looks that it works fine, but the TD element collection stays empty and the innerText is empty too. That while the data is showing in a visual browser. GetelementsbyTagName don't catch TDelements, and I can't figure out why ....
    
    
    Sub Extract_TD_Text()
        Dim URL As String
        Dim wMatrix As String
        Dim Search As String
        Dim IE As InternetExplorer
        Dim HTMLdoc As HTMLDocument
        Dim myTable As HTMLObjectElement
        Dim TDelements As IHTMLElementCollection
        Dim TDelement As HTMLTableCell
        Dim rij As Long
        Dim bGetNext As Boolean
        
        URL = "https://www.nasdaq.com/market-activity/stocks/arch"
        wMatrix = "sector;industry;1 year target;forward p/e 1 yr.;earnings per share(eps);annualized dividend;ex dividend date;beta"
        
        Set IE = New InternetExplorer
        With IE
            .Navigate2 URL
            .Visible = False 'or True it don't matter
            'Wait for page to load
            While .Busy Or .ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
            rij = 0
            bGetNext = False
            Set HTMLdoc = .Document
            If HTMLdoc.getElementsByTagName("table")(0).getAttribute("class") = "summary-data__table" Then
                Set myTable = HTMLdoc.getElementsByTagName("table")(0)
                Set TDelements = myTable.getElementsByTagName("td")
                For Each TDelement In TDelements
                    Search = LCase(Trim(TDelement.innerText))
                    If Search = "" Then
                        Search = "Nothing Found"
                    Else
                        Search = "*" & Search & "*"
                    End If
                    If bGetNext = True Then
                        Debug.Print TDelement.innerText 'Catch the data from the TDelement
                        bGetNext = False
                    End If
                    'Debug.Print TDelement.className '- useful for inspecting the HTML info
                    If wMatrix Like Search Then
                        Debug.Print TDelement.innerText 'Show the string where searching for
                        bGetNext = True 'Trigger to catch the data from the next TDelement
                    End If
                    rij = rij + 1
                Next
            End If
        End With
        IE.Quit
    End Sub


    Thanks in advance.
    Attached Images Attached Images

  7. #7
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    7,337
    Location
    Welcome to the forum

    Take a minute and read the FAQs in the link in my signature

    Now ...

    That was a 7 year old post. It's better it you start your own new thread using the [+Post New Thread] button top left

    Reference this one if it contains pertinent information
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    Thanks for the reply.
    My problem is, I don't know to which topic (category) my post belongs.
    It is very confusing for me. Maybe You can help me to choose which topic.
    Marcus

Posting Permissions

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