Wednesday, March 15, 2017

Excel VBA- YouTube Playlist links listing on Excel sheet

YouTube Playlist links listing on Excel sheet

Sometimes you need to scrap the links of all videos of YouTube of a playlist. To get all the links associated with the playlist page, we can use the following code:

Option Explicit

Sub pScrapWebPgLinks()
    Dim IE As New SHDocVw.InternetExplorer
    Dim strURL As Variant
    Dim HTMLDoc As HTMLDocument
    Dim TDelements As IHTMLElementCollection
    Dim ATags As IHTMLElementCollection
    Dim ATag As IHTMLElement
    Dim K As Integer
    strURL = Application.GetOpenFilename
    If strURL = False Then Exit Sub
    With IE
        .Visible = True
        .navigate strURL
    End With
   
    Do While IE.readyState <> READYSTATE_COMPLETE
        DoEvents
    Loop
    Set HTMLDoc = IE.document
    Set ATags = HTMLDoc.getElementsByTagName("a")
    Cells.Clear
    For Each ATag In ATags
        If ATag.parentElement.className = "pl-video-title" Then
        K = K + 1
            Range("A" & K).Value = K
            Range("B" & K).Value = ATag.innerText
            Range("C" & K).Value = ATag.getAttribute("href")
        End If
    Next
End Sub

No comments:

Post a Comment

Hot Topics