这应该可以做到。
doc.css('#incannualdiv > #fs-table tr')
at_css
(我想
at
)返回一个元素,其中
css
返回所有匹配项。
编辑
:对OP意见的答复
你可以在
tr
实际上是其子代中的文本
td
s通过使用
text
方法
trs = doc.css('#incannualdiv > #fs-table tr')
# Get column labels from table headers
labels = trs.first.css('th')[1..-1].map(&:text)
net_income_tr = trs.detect { |tr|
tr.css('td').any? { |td| td.text.strip =~ /^Net Income$/ }
}
# drop first tr that just has Net Income text
# I gave up on error checking. Exercise left to reader ;)
# this will give you an array of floats for your Net Income columns
net_income_columns = net_income_tr.css('td')[1..-1].map { |td| td.text.gsub(',','_').to_f }
labeled_values = net_income_columns.each_with_index.map { |value, i| { label: labels[i].strip, value: value } }