table tr tdhello/td tdimg src="xyz.png" width="100" height="100"/td /tr/tabletabledata.rows.each do |row| row.cells.each do |cell| puts cell.text endendputs "end" 获得输出 – helloend 我应该怎么做这样的输出 – helloxyz.pnge
<table>
<tr>
<td>hello</td>
<td><img src="xyz.png" width="100" height="100"></td>
</tr>
</table>
tabledata.rows.each do |row|
row.cells.each do |cell|
puts cell.text
end
end
puts "end"
获得输出 – >
hello end
我应该怎么做这样的输出 – >
hello xyz.png end
没有使用Nokogiri.
获取属性您可以使用Element#attribute_value方法获取元素的属性.例如,
element.attribute_value( ‘属性’)
对于许多标准属性,您还可以执行以下操作:
element.attribute
输出单元格文本或图像文本
假设单元格有文本或图像:
>您可以遍历单元格
>检查图像是否存在
>输出图像src(如果存在)
>否则输出单元格文本
这看起来像:
tabledata.rows.each do |row|
row.cells.each do |cell|
if cell.image.exists?
puts cell.image.src #or cell.image.attribute_value('src')
else
puts cell.text
end
end
end
puts "end"
