Web scrape html table individual cells into string array in VBA MS Access
Lester Miazga
21
Reputation points
Here is a code to scrape data from the table. I got stuck on trying to store the data in some kind of fashion for later use in MS Access to display on forms in my application. I tried to utilize string array, as shown in the second variant of the code below, however it did not do what I intended. Can anyone point me in a right direction, please?
Option Compare Database
Sub WeatherData()
Dim ie As New InternetExplorer
Dim doc As New HTMLDocument
'Initiate Internet Explorer
ie.Visible = False
ie.Navigate "https://forecast.weather.gov/MapClick.php?lat=41.963655000000074&lon=-87.80755499999998#.Y_eODCbMLRY"
Do While ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set doc = ie.Document
'grab a specific element
Dim CurCond As IHTMLElement
Set CurCond = doc.getElementsByClassName("myforecast-current").item
Dim CurTempF As IHTMLElement
Set CurTempF = doc.getElementsByClassName("myforecast-current-lrg").item
Dim CurTempC As IHTMLElement
Set CurTempC = doc.getElementsByClassName("myforecast-current-sm").item
Debug.Print "Current conditions: " & CurCond.innerText & "Current Temperature: " & _
CurTempF.innerText & " or " & CurTempC.innerText
Set Table = doc.getElementById("current_conditions_detail")
Set tRows = Table.getElementsByTagName("tr")
For Each r In tRows
Set tCells = r.getElementsByTagName("td")
For Each C In tCells
Debug.Print C.innerText
Next
Debug.Print ";"
Next
Debug.Print "------------------------"
End Sub
Here is the code in my attempt to store data in a string array:
Sub WeatherData()
Dim ie As New InternetExplorer
Dim doc As New HTMLDocument
'Initiate Internet Explorer
ie.Visible = False
ie.Navigate "https://forecast.weather.gov/MapClick.php?lat=41.963655000000074&lon=-87.80755499999998#.Y_eODCbMLRY"
Do While ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set doc = ie.Document
'grab a specific element
Dim CurCond As IHTMLElement
Set CurCond = doc.getElementsByClassName("myforecast-current").item
Dim CurTempF As IHTMLElement
Set CurTempF = doc.getElementsByClassName("myforecast-current-lrg").item
Dim CurTempC As IHTMLElement
Set CurTempC = doc.getElementsByClassName("myforecast-current-sm").item
Debug.Print "Current conditions: " & CurCond.innerText & "Current Temperature: " & _
CurTempF.innerText & " or " & CurTempC.innerText
Set Table = doc.getElementById("current_conditions_detail")
Set tRows = Table.getElementsByTagName("tr")
Dim tData(11) As String
For i = 0 To 11
For Each r In tRows
Set tCells = r.getElementsByTagName("td")
For Each C In tCells
Debug.Print C.innerText
Dim Ctext As String
Ctext = C.innerText
tData(i) = Ctext
Next
Debug.Print ";"
Next
Debug.Print "------------------------"
Next i
Debug.Print tData(0) & "; " & tData(1) & "; " & tData(2) & "; " & tData(3) & "; " & tData(4) & "; " & _
tData(5) & "; " & tData(6) & "; " & tData(7) & "; " & tData(8) & "; " & tData(9) & "; " & tData(10) & _
"; " & tData(11)
End Sub
And the following is a snapshot of the html code I am scraping:
Microsoft 365 and Office Development Other
4,374 questions
Microsoft 365 and Office Access Development
910 questions
Sign in to answer