VBA的新增功能,并且试图从仅在单击后才出现的网页上的表中提取数据,这是一个圆圈。”>
我在excel中使用VBA宏从weather.com提取数据:
问题:
有人介意看一下代码并帮助我吗?谢谢!
我可以成功提取“ day”,“ weather”和“ temp”,但不能提取“ srise”。
这是我的VBA代码:
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
这里是单击菜单并在源页面上显示出来的菜单的源HTML代码:
我正在尝试提取位于底部SPAN的7:05 am:
<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>
感谢您看一下,并帮助过头的完整新手!
-J
VBA的新增功能,并且试图从仅在单击后才出现的网页上的表中提取数据而绕行。我正在Excel中使用VBA宏从weather.com提取数据:我可以...
到目前为止,您做得不错,为此付出了+1的努力!