使用Java脚本从html表中提取元素

时间:2019-03-07 11:05:53

标签: javascript html web-scraping

<tbody class='detail' onclick='singleDelayCall(toggleDetail, event, this)'><tr><td Class='cluster' rowspan='539'>core</td>
<td Class='name' rowspan='539'>bilateral_1t_throttle0</td>
<td Class='result-name '>timing: cycles</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>31787.0</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>4967.0</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>33695.0</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>4967.0</td>
</tr><tr class='detail-hide'><td Class='result-name '>pmu: COUNTER0_OVERFLOW</td>
<td Class='' title='' lastPassTag=''>&nbsp;</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>0.0</td>
<td Class='' title='' lastPassTag=''>&nbsp;</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>0.0</td>
</tr><tr class='detail-hide'><td Class='result-name '>pmu: COUNTER2_OVERFLOW</td>
<td Class='' title='' lastPassTag=''>&nbsp;</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>0.0</td>
<td Class='' title='' lastPassTag=''>&nbsp;</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>0.0</td>
</tr><tr class='detail-hide'><td Class='result-name '>pmu: COMMITTED_PKT_ANY</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1997.0</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1997.0</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1997.0</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1997.0</td>

我只想提取功绩类别下的数字31787.0,4967.0,33695.0,4967.0。它们是<tr> class='cluster'</tr>的一部分。我不希望使用数字,但不要使用<tr> class='cluster'</tr>。 我已经尝试过了,但是没有用

var list = document.getElementsByClassName('cluster')[0];
zlen=list.getElementsByClassName('metric')[0].innerHTML; 
for (var i = 0; i <=10; i++) {
      z = zlen[i].innerHTML;
      document.writeln(z);

任何修改或建议,不胜感激。 谢谢

1 个答案:

答案 0 :(得分:0)

您只选择metric类的第一个元素! 您需要更改的内容:

var list = document.getElementsByClassName('cluster')[0];
zlen=list.getElementsByClassName('metric'); 
for (var i = 0; i <=10; i++) {
      z = zlen[i].innerHTML;
      document.writeln(z);

编辑:我为您做了一个小提琴JSFiddle

  

请注意:

     
      
  • 我已将Class更改为class

  •   
  • 并将class='cluster'td更改为tr

  •