退休< TD>标签从网站上的表使用VBA并放入excel [英] Retreiving <TD> tag from table on website using VBA and put into excel

查看:186
本文介绍了退休< TD>标签从网站上的表使用VBA并放入excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to retrieve information from a <TD> tag on a website.

它的工作原理,但似乎无法从< td> 标签中获取< TR> 标签,而使用条件语句来获取第二个标签,这是他唯一的方式,我看到这是有效的。该代码工作正常提取信息我只是无法弄清楚如何访问该秒,条件是我在第一个< td> 中找到匹配的条件。

It works but I cant seem to get the text from the second <td> tag in a <TR> tag while using a conditional statement to get the second tag as this is he only way I see that works. The code works fine to extract information I just cant figure out how to access that second with the condition that I have found a match in the first <td>.

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

So the actual html table would look like this.

<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脚本是:

The VBA script I have is:

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表单中显示系统名称:系统首字母缩略词:

This just displays System Name: and System Acronym: in the excel sheet

推荐答案

如果你有一个 td 元素,你想得到内部文本然后使用下一个 td ,然后使用 nextSibling 属性,如下所示:

If you have a td element and you want to get the inner text of the next td in the row then use the nextSibling property, like this:

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首先有系统名称和系统首字母缩略词第二,而代码似乎被构造为首先查找系统首字母缩略词第二个系统名称第二个

Note that nothing in the given code is changing the value of excelRow so everything will keep getting written into the same row. Also note that the HTML given has the "System Name" first and the "System Acronym" second whereas the code seems to be structured to look for "System Acronym" first and "System Name" second

这篇关于退休&lt; TD&gt;标签从网站上的表使用VBA并放入excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆