FractalWalk
Technical User
I am trying to loop through a list of NFL players and extract stats from a website. I originally was using the InternetExplorer.application object but it was waaaay too slow and often run into problems with the page loads. After searching online I think using MSXML2.XMLHTTP60 is a quicker, cleaner way to get the data but I am having trouble parsing the html code to find what I want.
I have verified that the html is being returned correctly (I printed it), but when I try to use getElementsByTagName it doesn't find the tags. I'm new to XMLHTTP60, but I followed the steps I found on multiple sites of assigning the .repsonseText to an HTMLBody object. I would assume that would allow me to use .getElementsByClassName and .getElementsByTagName like I would with InternetExplorer.Document. But even though I am using the same code from these working examples, I can't get the code to see the tags.
I have verified that the html is being returned correctly (I printed it), but when I try to use getElementsByTagName it doesn't find the tags. I'm new to XMLHTTP60, but I followed the steps I found on multiple sites of assigning the .repsonseText to an HTMLBody object. I would assume that would allow me to use .getElementsByClassName and .getElementsByTagName like I would with InternetExplorer.Document. But even though I am using the same code from these working examples, I can't get the code to see the tags.
Code:
Public Sub test()
Dim IE As MSXML2.XMLHTTP60
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLBody As MSHTML.HTMLBody
Dim [URL unfurl="true"]www As[/URL] String, txt As String
Dim nme
Dim objCollection As Object
Dim n As Integer
[URL unfurl="true"]www =[/URL] "[URL unfurl="true"]http://www.pro-football-reference.com/search/search.fcgi?search="[/URL]
'this is an array of names read from Ecxcel
nme = Cells(1, 1).CurrentRegion
Set IE = New MSXML2.XMLHTTP60
'Loop through all the names
For x = 1 To UBound(nme)
'search the website by name
IE.Open "GET", [URL unfurl="true"]www &[/URL] nme(x, 1), False
IE.send
While IE.ReadyState <> 4
DoEvents
Wend
Set HTMLDoc = New MSHTML.HTMLDocument
Set HTMLBody = HTMLDoc.body
HTMLBody.innerHTML = IE.responseText
'The error occurs with the following line. No title tags are found (but there really is one)
'and so the obCollection is not set. I have verified that the title tag exists as I printed it
'and saw it and using Split(IE.responseText,"<title>") correctly gives an array of two elements (i.e. one title tag).
'extract the title tag to verify that the player page was returned
Set objCollection = HTMLBody.getElementsByTagName("title")
'NOTHING IS RETURNED FORM THE ABOVE LINE AND SO THE NEXT LINE ERRORS "Object variable not set"
txt = objCollection(0).innerText
If txt Like "*" & nme(x, 1) & "*" Then
'code for player found
Else
'code for player not found
End If
Next x
End Sub