从单击后显示的隐藏菜单中提取WEB PAGE数据

问题描述 投票:2回答:1

VBA的新增功能,并且试图从仅在单击后才出现的网页上的表中提取数据,这是一个圆圈。”>

我在excel中使用VBA宏从weather.com提取数据:

  1. 我可以成功地从可见表中提取元素。
  2. 当我试图从隐藏菜单中拉出“日出”和“日落”时间时,我拉出错误91。
  3. 我想从表的第二天开始提取日出和日落的时间。
  4. 问题:

  1. 是否可以在代码中单击以显示我要提取的数据?
  2. 此数据位于源代码的SPAN中-我可以从最接近问题的SPAN的ClassName中提取此innerText吗?
  3. 有人介意看一下代码并帮助我吗?谢谢!

我可以成功提取“ 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提取数据:我可以...

html excel vba extract
1个回答
0
投票

到目前为止,您做得不错,为此付出了+1的努力!

© www.soinside.com 2019 - 2024. All rights reserved.