nokogiri解析第一个td in tr忽略特定的类

时间:2017-09-29 05:27:39

标签: ruby parsing nokogiri

我有以下html

<table>
    <tr>
      <th>value</th>
      <th>description</th>
    </tr>
    <tr>
      <td>OverallHealthScore</td>
      <td>

             Overall HealthScore.

      </td>
    </tr>
    <tr>
      <td class="deprecated">DESTAGED_TRACKS_PER_SEC</td>
      <td>

             The tracks per second saved into disks.

      </td>
    </tr>
</table>

有许多tr,但这是两个场景的摘录 我只需要打印出OverallHealthScore

table.css('tr').map do |row|
    puts row.css('td:not(.deprecated)').map(&:text)[0]
end

在那里找我,但在不推荐使用的项目上打印出“description”td。我似乎无法弄清楚我需要做些什么来获得我需要的结果。

1 个答案:

答案 0 :(得分:1)

假设您想获得第一个未弃用的td值:

<table>
  <tr>
    <th>value</th>
    <th>description</th>
  </tr>
  <tr>
    <td>OverallHealthScore</td>
    <td>

      Overall HealthScore.

    </td>
  </tr>
  <tr>
    <td class="deprecated">DESTAGED_TRACKS_PER_SEC</td>
    <td>

      The tracks per second saved into disks.

    </td>
  </tr>
  <tr>
    <td>AvaiableAnother</td>
    <td>

      Another Available HealthScore.

    </td>
  </tr>
  <tr>
    <td class="deprecated">OTHER_DEPRE</td>
    <td>

      The tracks per second saved into disks.

    </td>
  </tr>
</table>

然后

puts table.css('td:first-child:not(.deprecated)').map(&:text)
# OverallHealthScore
# AvaiableAnother