0

The code below successfully loops through each element in the DOM and puts each element in an Excel sheet. (tagName, ID, className etc.)

My question, is:

How can I scrape the tag attibutes (title, href etc.) for each element? Specifically, for an "A" tag, how can I scrape the "href" attribute?

Enum READYSTATE
    READYSTATE_UNINITIALIZED = 0
    READYSTATE_LOADING = 1
    READYSTATE_LOADED = 2
    READYSTATE_INTERACTIVE = 3
    READYSTATE_COMPLETE = 4
End Enum

Dim ie As InternetExplorer
Dim html As HTMLDocument
Dim RowNumber As Integer
Set ie = New InternetExplorer

ie.Visible = False
ie.navigate "www.somesite.com"

Do While ie.READYSTATE <> READYSTATE_COMPLETE
    Application.StatusBar = "Connecting..."
    DoEvents
Loop

Set html = ie.document

RowNumber = 1
For Each element In html.all
    Cells(RowNumber, "A").Value = element.tagName
    Cells(RowNumber, "B").Value = element.ID
    Cells(RowNumber, "C").Value = element.className
    Cells(RowNumber, "D").Value = element.innerHTML
    RowNumber = RowNumber + 1
Next element

Any help would be appreciated.

1 Answer 1

2

Add this line before RowNumber = RowNumber + 1:

If (element.tagName = "A") Then Cells(RowNumber, "E").Value=element.getAttribute("href")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.