如何 select 仅 table 具有特定内容的行

How to select only table rows with specific content inside

我正在抓取一封包含许多 table 行的电子邮件,其中一些我想排除。我需要的 table 行看起来 完全 像:

<tr>
  <td class="quantity"> ANYTHING BUT EMPTY </td>
  <td class="description"> ANYTHING BUT EMPTY </td>
  <td class="price"> ANYTHING BUT EMPTY </td>
</tr>
table 行中的

None 具有 class 或 ID。此外,有不需要的 <table> 行包含具有这些 classes 的单元格,但有些行没有值,因此我只需要获得具有这三个 classes 的 table 行的单元格,以及所有三个具有非空值的单元格。我不确定执行此操作的语法:

body = Nokogiri::HTML(email)
wanted_rows = body.css('tr').select{ NOT SURE HOW TO ENCAPSULATE LOGIC HERE }

这对于 XPath 来说相当简单:

wanted_rows = body.xpath('//tr[td[(@class = "quantity") and normalize-space()]
  and td[(@class = "description") and normalize-space()]
  and td[(@class = "price") and normalize-space()]]')

normalize-space() 调用实际上与 normalize-space(.) != "" 相同,即它们检查当前节点(td)是否包含空格以外的内容。