如何从表格行中选择th或td? [英] How do I select either a th or a td from a table row?
本文介绍了如何从表格行中选择th或td?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Rails 5中使用Nokogiri 5.如何从表格行中选择th元素或td元素?我的目标是将所有的单元格文本都放在一行中(如果有更通用,更优雅的解决方案,我全部都在)。这就是我所拥有的
I'm using Nokogiri with Rails 5. How do I select either a "th" element or a "td" element from a table row? My goal is to get all the text of cells in a row (if there is a more generic, elegant solution, I'm all in). Here's what I have
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对于从行中选择th或td元素是无效的语法。
As you may have noticed "td | th" is not valid syntax for selecting the "th" or "td" elements from the row.
推荐答案
使用,
(逗号)选择多个节点:
Use a ,
(comma) to select multiple nodes:
row_values = row.css('td, th').map{|str| str.text }
这篇关于如何从表格行中选择th或td?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文