使用VBA从网站上的表格中恢复标记并放入excel

时间:2017-04-17 19:30:43

标签: html excel vba excel-vba conditional

我正在尝试从网站上的<TD>标记中检索信息。

它可以工作,但我似乎无法从<td>标记中的第二个<TR>标记中获取文本,同时使用条件语句来获取第二个标记,因为这是他唯一可行的方式。代码可以很好地提取信息我只是想知道如何在第一个<td>中找到匹配的条件访问第二个。

所以实际的html表看起来像这样。

<html>
<head></head>
<body>
<table id="Table2">
<tr>
  <td class="tSystemRight">System Name: -if this matches</td>
  <td class="tSystemLeft breakword">Windows3756 -I need this</td>
</tr>
<tr>
  <td class="tSystemRight">System Acronym: -if this matches</td>
  <td class="tSystemLeft breakword">WIN37  -I need this</td>
</tr>
</table>
</body>
</html>

我的VBA脚本是:

excelRow = 2

For Each tr In msxml.tableRows
cellCount = 1
   For Each TD In tr.getElementsByTagName("TD")
    If ((cellCount = 1) And (TD.innerText = "System Acronym:")) Then
       Worksheets("Data").Cells(excelRow, 2).value = Cells(1, 2)
    ElseIf ((cellCount = 1) And (TD.innerText = "System Name:")) Then
       Worksheets("Data").Cells(excelRow, 3).value = Cells(1, 2)
    cellCount = cellCount + 1
    End If
   Next
Next

这只会在Excel工作表中显示System Name:System Acronym:

2 个答案:

答案 0 :(得分:3)

如果您有td元素,并且想要获取行中下一个td的内部文本,请使用nextSibling属性,如下所示:

For Each td In tr.getElementsByTagName("TD")
    If ((cellCount = 1) And (td.innerText = "System Acronym:")) Then
       Worksheets("Data").Cells(excelRow, 2).Value = td.NextSibling.innerText
    ElseIf ((cellCount = 1) And (td.innerText = "System Name:")) Then
       Worksheets("Data").Cells(excelRow, 3).Value = td.NextSibling.innerText
    cellCount = cellCount + 1
    End If
   Next
Next

请注意,给定代码中的任何内容都不会更改excelRow的值,因此所有内容都会被写入同一行。另请注意,给出的HTML具有&#34;系统名称&#34;首先和&#34;系统缩写&#34;第二,而代码似乎是为了寻找&#34;系统缩写&#34;首先和&#34;系统名称&#34;第二

答案 1 :(得分:2)

我从一个与您的结构几乎完全相同的公共网站开发了以下内容。 (Event Delegation

需要参考Option Explicit Sub Test() Dim ie As New InternetExplorer Dim doc As New HTMLDocument With ie .Visible = True .Navigate "https://www.federalreserve.gov/releases/h3/current/" 'can place code to wait for IE to load here .. I skipped it since its not in direct focus of question Set doc = .Document Dim t As HTMLTable Dim r As HTMLTableRow Dim c As HTMLTableCol Set t = doc.getElementById("t1tg1") 'loop through each row For Each r In t.Rows If r.Cells(0).innerText = "Mar. 2016" Then Debug.Print r.Cells(1).innerText 'loop through each column in the row 'For Each c In r.Cells ' Debug.Print c.innerText 'Next Next End With End Sub For Each r In t.Rows 'find out which columns System Acronym and value will be and modify the Cells(n) statements If r.Cells(0).innerText = "System Acronym:" Then Worksheets("Data").Cells(excelRow, 2).Value = r.Cells(2).innerText Next

function onSubmit() {
  var tev = setInterval(move, 500);
  if (animate == false) {
    setInterval(move, 500);
    animate = true;
  } else {
    clearInterval(tev);
    animate = false;
  }
}
<input type="button" onclick="onSubmit();" value="Shoot"/>

所有这一切,在设置了我上面的特定表后,我建议您对代码进行以下编辑(我已经省略了cellcount检查和其他内容):

{{1}}