【发布时间】:2016-09-06 02:50:34
【问题描述】:
在此示例中,我尝试从表格的 <td> 标记中获取文本。一、html代码。
<table>
<tbody>
<tr>
<td>Single line of text</td>
</tr>
<tr>
<td>Text here<p>First line</p><p>Second line</p></td>
</tr>
</tbody>
</table>
然后是这里的ruby代码。
require 'nokogiri'
require 'pp'
html = File.open('test.html').read
doc = Nokogiri::HTML(html)
rows = doc.xpath('//table[1]/tbody/tr')
data = rows.collect do |row|
row.at_xpath('td[1]/text()').to_s
end
pp data
我得到的结果是。
["Single line of text", "Text here"]
如何获取第二个<td> 标签中的所有文本?
【问题讨论】: