如何从 table 行中 select th 或 td?
How do I select either a th or a td from a table row?
我将 Nokogiri 与 Rails 一起使用 5. 如何 select 来自 table 行的 "th" 元素或 "td" 元素?我的目标是获取一行中所有单元格的文本(如果有更通用、更优雅的解决方案,我全力以赴)。这是我的
text_all_rows = all_rows.map do |row|
row_values = row.css('td | th').map{|str| str.text }
.map{|str| str.gsub(/[[:space:]]+/, ' ').gsub(/\A\p{Space}+|\p{Space}+\z/, '') }.join("\t")
[*row_values]
end
您可能已经注意到,"td | th" 对于 select 行中的 "th" 或 "td" 元素来说不是有效语法。
使用,
(逗号)到select多个节点:
row_values = row.css('td, th').map{|str| str.text }
我将 Nokogiri 与 Rails 一起使用 5. 如何 select 来自 table 行的 "th" 元素或 "td" 元素?我的目标是获取一行中所有单元格的文本(如果有更通用、更优雅的解决方案,我全力以赴)。这是我的
text_all_rows = all_rows.map do |row|
row_values = row.css('td | th').map{|str| str.text }
.map{|str| str.gsub(/[[:space:]]+/, ' ').gsub(/\A\p{Space}+|\p{Space}+\z/, '') }.join("\t")
[*row_values]
end
您可能已经注意到,"td | th" 对于 select 行中的 "th" 或 "td" 元素来说不是有效语法。
使用,
(逗号)到select多个节点:
row_values = row.css('td, th').map{|str| str.text }