I have been toying with a VBA project in excel. The idea is that it will scrape a website (specified in the spreadsheet) and pull data from the site. In this example I am pulling playstation data (i.e. Games played, time played, trophies, etc.) and putting them into a spreadsheet. I have a total of 69 games but the scraper only pulls 50.
How do I get it to pull in all 68 (or more in the future?)
I'll admit, I am not very versed with VBA, so any assistance would be greatly appreciated.
My source code:
Sub scrape_quotes()
Set browser = CreateObject("InternetExplorer.Application")
'Dim browser As InternetExplorer
Dim Games As Object
Dim Game As Object
Dim Num As Long
Dim DateLastPlayed As Object
Dim PlatformType As Object
Dim BronzeNum As Object
Dim SilverNum As Object
Dim GoldNum As Object
MsgBox "Please wait, this may take a few minutes..." & vbNewLine & "Pres OK To Continue", vbInformation, "Game Tracker"
Application.StatusBar = "Scraping Data. Please wait..."
' Assigns a cell for the URL
Dim URL As String
URL = ThisWorkbook.Sheets("Scraper").Range("B6").Value
If Len(URL) = 0 Then Exit Sub
' Opens "invisible" browser and remains until all data is loaded
'Set browser = New InternetExplorer
browser.Visible = True
browser.Navigate URL
Do While browser.readyState <> 4 Or browser.Busy: DoEvents: Loop
browser.Document.parentWindow.scroll 0&, 20000&
On Error GoTo ErrHandler
' Looks for data in the "box" element on website
Set Games = browser.Document.getElementsByClassName("box")
Dim GameName As String, Hoursplayed As String
' Looks for data in the "lastplayed" element on website
Set DateLastPlayed = browser.Document.getElementsByClassName("lastplayed")
Dim Lastplayed As String
' Looks for data in the "platforms" element on website
Set PlatformType = browser.Document.getElementsByClassName("platforms")
Dim Platform As String
' Looks for data in the "bronze" element on website
Set BronzeNum = browser.Document.getElementsByClassName("bronze")
Dim Bronze As String
' Looks for data in the "silver" element on website
Set SilverNum = browser.Document.getElementsByClassName("silver")
Dim Silver As String
' Looks for data in the "gold" element on website
Set GoldNum = browser.Document.getElementsByClassName("gold")
Dim Gold As String
' Assigns which sheet to parse data do
Dim WS As Worksheet
Set WS = ThisWorkbook.Sheets("Games List")
' Assigns each column used for each category
Application.ScreenUpdating = False
For Each Game In Games
CleanData Game.innerText, GameName, Hoursplayed, Lastplayed, Platform, Bronze, Silver, Gold
If Len(GameName) Then
Num = Num + 1
WS.Cells(1 + Num, 1).Value = GameName
WS.Cells(1 + Num, 2).Value = Hoursplayed
WS.Cells(1 + Num, 4).Value = Platform
End If
GameName = "": Hoursplayed = ""
Next
'New code starts here.
Num = 0
For Each Line In DateLastPlayed
If Len(Line) Then
Num = Num + 1
WS.Cells(1 + Num, 3).Value = Line.innerText
End If
Next
Num = 0
For Each Line In PlatformType
If Len(Line) Then
Num = Num + 1
WS.Cells(1 + Num, 4).Value = Line.innerText
End If
Next
Num = 0
For Each Line In BronzeNum
If Len(Line) Then
Num = Num + 1
WS.Cells(1 + Num, 5).Value = Line.innerText
End If
Next
Num = 0
For Each Line In SilverNum
If Len(Line) Then
Num = Num + 1
WS.Cells(1 + Num, 6).Value = Line.innerText
End If
Next
Num = 0
For Each Line In GoldNum
If Len(Line) Then
Num = Num + 1
WS.Cells(1 + Num, 7).Value = Line.innerText
End If
Next
ErrHandler:
If Err.Number = 0 Then Debug.Print Err.Number & vbNewLine & Err.Description
Application.ScreenUpdating = True
browser.Quit
Set browser = Nothing
MsgBox "Game Data Has Been Scraped!", vbExclamation, "Game Tracker"
Application.StatusBar = False
Sheets("Games List").Activate
End Sub
This is the URL I have it pulling data from:
I've tried forcing IE to scroll to the bottom of the page before completing, I tried adding a wait timer, all to no avail.