Ruby:如何从html表中获取数据

时间:2014-10-03 05:35:51

标签: html ruby

我是Ruby新手,有人可以帮助我吗?

下面是我的html结构,我想打印' X01FJ65K0M'来自td。有 在这张桌子上没有id。

提前致谢



<table class="results" width="100%">
<tbody><tr>
<th class="results" style="">
Request Type
</th>

<th class="results" style="">
Agent Type
</th>

<th class="results" style="">
</th>
</tr>
<tr class="results0">
<td style="">

X01FJ65K0M

</td>
<td style="">
07/03/2014 08:14:42
</td>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

Nokogiri与css选择器一起使用:

html = <<EOS
<table class="results" width="100%">
<tbody><tr>
<th class="results" style="">
Request Type
</th>

<th class="results" style="">
Agent Type
</th>

<th class="results" style="">
</th>
</tr>
<tr class="results0">
<td style="">

X01FJ65K0M

</td>
<td style="">
07/03/2014 08:14:42
</td>
</tr>
</table>
EOS

require 'nokogiri'
doc = Nokogiri.HTML(html)
doc.at_css('.results0/td[1]').text.strip
# => "X01FJ65K0M"

答案 1 :(得分:0)

使用jquery:

$(function(){
    var str = $('table tr td:first').text();
    alert(str);
})

演示:http://jsfiddle.net/m5Lrseup/