0
我试图用引入nokogiri使用下面的代码从HTML文件中提取数据:引入nokogiri和嵌套块
@doc = Nokogiri::HTML("<table >
<tr BGCOLOR=\"#eeeeee\">
<td>SPILLED</td>
</tr>
<tr BGCOLOR=\"#eeeeee\">
<td >RUSTING</td>
</tr>
</table>")
@doc.xpath('//tr[@bgcolor="#eeeeee"]').each do |record|
print record
record.xpath("//td").each do |cell|
print cell
end
end
第一个块似乎是按预期工作,通过record
每次只包含一个的行。另一方面,第二个块正在为两行访问<td>
元素,这对我来说很神秘,因为record
变量在进入内部块之前显示它只有一行数据。
“记录”在两个行中的数据在内部块时如何?
可能的重复:http://stackoverflow.com/questions/12301611/nokogiri-xpath-attribute-strange-results/12301766 –
@shioyama你是对的。到OP:从你的单元格选择器中删除前导斜线,并且你会得到你期望的结果。 –