kamelkid2
kamelkid2

Reputation: 534

Scrape the innertext from an "a href" in an HTML page with EXCEL VBA

I am trying to scrape data from a website with the following HTML code

<a href='https://somesite.com/nation/id=344'>Vee Veetis <img src='https://somesite.com/img/flags/albania.jpg' class='tinyflag'></a><br />FireBird </td>

I have the following VBA

    With IE.document

    Set elems = .getElementsByTagName("a")
    For Each e In elems

        If e Like "https://somesite.com/record/id=*" Then
            Sheets("Members").Range("A" & i).Value = e
            Sheets("Members").Range("B" & i).Value = e.innerText ' doesnt work, returns "view" - desire 'Vee Veetis'
            Sheets("Members").Range("C" & i).Value = e.outerText ' doesnt work, returns "view" - desire 'Firebird'
            i = i + 1
            Exit For ' remove this to scrape remaning items once working
        End If

    Next e

    End With

I am able to scrape the actual link without problem, but I am struggling to find how I can reference the Text that contains the link "Vee Veetis" and the corresponding text 'Firebird' that is directly after the link. Does anybody have guidance on how these are related and can be efficiently scraped?

Upvotes: 1

Views: 2934

Answers (1)

Hubvill
Hubvill

Reputation: 504

You could use the code bellow to extract the data in the < td> tag that contains "Vee Veetis". Keep in mind "Vee Veetis" and "Firebird" are in the same < td> tag so both values will be returned in A1 and they will be separated by a line break. But you could store the result in a string then split the string by linebreak to return "Vee Veetis" or "Firebird".

Set elems = IE.document.getElementsByTagName("td")
For Each e In elems

    If e.innerText Like "*Vee Veetis*" Then
    Range("A1").Value = e.innerText
    End If

Next e

Hope that helps.

Upvotes: 1

Related Questions