New to VBA and going in circles trying to extract data from a table on a webpage that only appears after it is clicked.
I'm using a VBA macro within excel to pull data from a weather.com:
- I can successfully extract elements from the visible table.
- I'm pulling Error 91 when trying to pull the "sunrise" and "sunset" time from the hidden menu.
- I would like to extract the time of sunrise and sunset from the 2nd day on the table.
Question:
- Is there a way to click within the code to reveal the data I want to extract?
- This data is located within a SPAN in the source code - Can I be extract this innerText from the closest ClassName to the SPAN in question?
Would somebody mind taking a look at the code and helping me out? Thanks!
I can successfully extract "day", "weather", and "temp" but cannot pull "srise".
Here is my VBA code:
Sub Get_Lancaster()
Dim request As Object
Dim response As String
Dim html As New HTMLDocument
Dim website As String
Dim weather As Variant
Dim temp As Variant
Dim day As Variant
Dim srise As Variant
website = "https://weather.com/weather/5day/l/2db548c2f0fb03c25c0d5c5520a32877082d295d907b06df5eff91cd140165b9"
Set request = CreateObject("MSXML2.XMLHTTP")
request.Open "GET", website, False
request.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
request.send
response = StrConv(request.responseBody, vbUnicode)
html.body.innerHTML = response
weather = html.getElementsByClassName("description").Item(2).innerText
temp = html.getElementsByClassName("temp").Item(2).innerText
day = html.getElementsByClassName("day-detail clearfix").Item(1).innerText
srise = html.getElementsByClassName("sunrise").Item(1).innerText
Range("B3").Value = day
Range("D3").Value = weather
Range("E3").Value = temp
Range("G3").Value = srise
End Sub
Here is the source HTML code for the menu after it is clicked and revealed on the source page:
I am trying to extract the 7:05am located in the bottom SPAN:
<tr classname="clickable open" class="clickable open">
<td class="twc-sticky-col cell-hide">
<div classname="twc-table-shadow sticky" class="twc-table-shadow sticky"></div>
</td>
<td headers="uv-index" title="Partly cloudy skies. High around 40F. Winds light and variable." data-track-string="ls_24_hour_ls_24_hour_toggle" classname="uv" class="uv">
<span class="">3 of 10</span>
</td>
<td headers="sunrise" title="Partly cloudy skies. High around 40F. Winds light and variable." data-track-string="ls_24_hour_ls_24_hour_toggle" classname="sunrise" class="sunrise">
<div>
<span classname="icon icon-font iconset-astro dark icon-sunrise" class="icon icon-font iconset-astro dark icon-sunrise">
</span>
<span>7:05 am</span>
</div>
</td>
Thanks for taking a look and helping a complete newbie who's in over their head!
-J

