Despite lots of research and testing re scraping I'm not able to download data from the target site, Gold Reserves by Country 2021 | World Gold Council
The code is succesful with other sites which are included in the code for testing purposes only.
The eventual aim is not to download all data, but 5 trs namely, USA, GBR, RUS, CHN and POL and 1 td namely, 'Gold Reserves Tonnes'. I guess that can be done with something like element names but I'm stuck at the start. Maybe the issue is a simple one of parent / child but I am not able to resolve and would be grateful of any help to move on.
The code is succesful with other sites which are included in the code for testing purposes only.
The eventual aim is not to download all data, but 5 trs namely, USA, GBR, RUS, CHN and POL and 1 td namely, 'Gold Reserves Tonnes'. I guess that can be done with something like element names but I'm stuck at the start. Maybe the issue is a simple one of parent / child but I am not able to resolve and would be grateful of any help to move on.
VBA Code:
Sub Get_Gold_Data()
Dim oDom As Object
Dim sURL As String
Dim oTable As Object, tbl As Object, oBody As Object, bod As Object, oTR As Object, tr As Object, oTD As Object, td As Object
Dim r As Long, c As Long
'target site fails using TagName and ClassName identifiers
sURL = "https://www.gold.org/goldhub/data/gold-reserves-by-country"
'fails using generic name table as there is no object named table
' sURL = "https://www.barchart.com/options/price-history/daily-prices?symbol=msft&strikePrice=155.00&symbolType=C&expirationDate=2019-12-20"
'ok
' sURL = "https://en.wikipedia.org/wiki/List_of_FIFA_World_Cup_winners"
'ok
'sURL = "http://www.bom.gov.au/products/IDN60701/IDN60701.95929.shtml"
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", sURL, False
.send
If .readyState = 4 And .Status = 200 Then
Set oDom = CreateObject("htmlfile")
oDom.body.innerHTML = .responseText
Else
MsgBox "Ready state: " & .readyState & vbLf & "HTTP request status: " & .Status, vbExclamation, "Error"
Exit Sub
End If
End With
r = 1
Set oTable = oDom.getElementsByTagName("table") 'generic name
' Set oTable = oDom.getElementsByClassName("table table-sm table-hover Qr4mW7qUIzc8plircFzmHg==") 'target site table name
For Each tbl In oTable 'target site fails here - oTable has been created - tbl is nothing
Set oBody = tbl.getElementsByTagName("tbody")
' Set oBody = oDom.getElementsByTagName("tbody")'other testing
For Each bod In oBody
Set oTR = bod.getElementsByTagName("tr")
' Set oTR = oDom.getElementsByTagName("tr")'other testing
For Each tr In oTR
Set oTD = tr.getElementsByTagName("td")
c = 1
For Each td In oTD
Sheet1.Cells(r, c).Value = td.innerText
c = c + 1
Next td
r = r + 1
Next tr
Next bod
Next tbl
''tidy up
Set oTable = Nothing: Set tbl = Nothing: Set oBody = Nothing: Set bod = Nothing
Set oTR = Nothing: Set oTD = Nothing: Set tr = Nothing: Set td = Nothing
End Sub