-2

I am getting a 462 error (the remote server does not exist or is unavailable):

Set QuestionField = html.getElementsByClassName("view-count style-scope yt-view-count-renderer")

I'm pretty sure the view information I'm trying to pull is in a class tag, and I am unsure why getElementsByClassName isn't working to pull this information.

Here is the relevant HTML code from YouTube:

<
span class="view-count style-scope yt-view-count-renderer">952 views
<
/span>

Here is the VBA Code:

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

Sub ImportYTData()
Dim ie As InternetExplorer
Dim html As HTMLDocument

Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "https://www.youtube.com/watch?    v=0YNJQNpP9Do&list=PL_Lt8vbVLfk_pzt-TWzfk_GNAKp-ePXc1&index=22"
Do While ie.READYSTATE <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to go to the YouTube Video ..."
DoEvents
Loop

Set html = ie.document
MsgBox html.DocumentElement.innerHTML

Application.StatusBar = ""

Dim RowNumber As Long
Dim QuestionField As IHTMLElement
Dim views As String

Set QuestionField = html.getElementsByClassName("view-count style-scope yt-view-count-renderer")
RowNumber = 4

views = QuestionField.innerText
views = Replace(views, "views", "")
views = Replace(views, "view", "")
Cells(RowNumber, 3).Value = Trim(views)

Set html = Nothing

End Sub
QHarr
  • 83,427
  • 12
  • 54
  • 101
Blake
  • 103
  • 1
  • 5

1 Answers1

1

Notice that you are calling a function getElements which is plural. This function is returning a collection which your are trying to assign to a single element. In other words, there is a mismatch.

Set QuestionField = html.getElementsByClassName("view-count style-scope yt-view-count-renderer")

One solution would be to change your DIM statement to:

Dim QuestionField As IHTMLElementCollection
Brian M Stafford
  • 8,483
  • 2
  • 16
  • 25
  • Thank you! Now I am getting a 462 error with Set QuestionField = html.getElementsByClassName("view-count style-scope yt-view-count-renderer") – Blake Sep 26 '17 at 13:27
  • 1
    The call to `getElements` is returning nothing and your code is assuming something will be returned. You need to add more code to handle this situation. Also, remember a collection is being returned so you'll need to either loop through the results, or pull off the first element, before accessing properties. – Brian M Stafford Sep 26 '17 at 13:46