<TD> 使用VBA从网站上的表格中检索标签并放入excel中



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

它有效,但我似乎无法从<TR>标签中的第二个<td>标签中获取文本,同时使用条件语句获取第二个标签,因为这是我看到的唯一方式。该代码可以很好地提取信息,我只是无法弄清楚如何使用我在第一个<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:

如果您有一个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首先具有"系统名称"one_answers"系统缩写"第二,而该代码似乎是结构化的,以查找"系统缩写"第一个和"系统名称"第二

我从与您的结构几乎相同的公共网站开发了以下内容。(https://www.federalreserve.gov/releases/h3/current/(

需要参考Microsoft Internet ControlsMicrosoft HTML Object Library

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

所有话都如上所述,在设置您的特定表之后,我建议对您的代码进行以下编辑(我遗漏了Cellcount检查和其他内容(:

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

最新更新