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

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

它的工作原理,但我似乎无法从第二个<td>标签中的文本从<TR>标签,而使用条件语句获得第二个标签,因为这是他唯一的方式,我看到的作品。 代码工作正常,以提取信息,我只是无法弄清楚如何访问第二个条件,我已经find了匹配在第一个<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 

这只是显示System Name:System Acronym:在Excel表格中

如果你有一个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首先有“系统名称”和“系统首字母缩写词”,而代码似乎是结构化的,首先查找“系统首字母缩写词”和第二个“系统名称”

我从一个与你的结构几乎完全相同的公共网站开发了以下内容。 ( 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 

所有说,设置你的具体表,像我上面,我build议你的代码的下面的编辑(我已经省略了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