正在解析 HTML 个文档

Parsing HTML document

我正在尝试使用 Ruby 和 Nokogiri 解析以下 HTML:

<div class="vevent">
<table width="750"><tr>
<td width="25"> </td>
<td valign="top" width="200">
<font size="2" face="sans-serif">
<font color="black"><b>June 30, 2015</b></font>
<br>
<span class="dtstart"><span class="value-title" title="2015-06-30"></span></span><br><span class="summary"><font color="#92161" size="3"><b>Band Concert</b></font></span>
<br><font color="#333333">Event</font><br>
<br>
<br>
<br clear="left">Have a question? email us.<br>
<br></font>
</td>
<td valign="top" width="10"></td>
<td valign="top">
<br clear="left"><font color="#92161">111 Main Street</font><br>
<font color="#92161">Mainstreet, Ohio 55111</font>
<a rel="nofollow" href="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=%221700+111+MainStreet+NE+Mainstreet,+Ohio+55111%22" target="_blank"><font size="1" face="sans-serif">map link</font></a><br><br>
<font color="#92161"><font size="2" face="sans-serif">Telephone:</font> 3305551000</font><br><br>
Visit our website for complete information.<br><br>
Enjoy a summer evening concert on Main Street at 8pm. Doors and cash bar open at 7pm.<br><br>Look for more details and ticket sales to be released soon on our website<br>  <br><br>
<br>
</td>
</tr></table>
</div>

我正在尝试获取最后一点文本:

Visit our website for complete information.<br><br>
Enjoy a summer evening concert on Main Street at 8pm. Doors and cash bar open at 7pm.<br><br>Look for more details and ticket sales to be released soon on our website<br>  <br><br>

到目前为止,这是我的代码:

events = doc.css("div.vevent")
events.collect do |row|
  row.css("td")[3]  
end

这将使我进入第三个 td,其中包含我要查找的文本,如下所示:

<td valign="top">
<br clear="left"><font color="#92161">111 Main Street</font><br>
<font color="#92161">Mainstreet, Ohio 55111</font>
<a rel="nofollow" href="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=%221700+111+MainStreet+NE+Mainstreet,+Ohio+55111%22" target="_blank"><font size="1" face="sans-serif">map link</font></a><br><br>
<font color="#92161"><font size="2" face="sans-serif">Telephone:</font> 3305551000</font><br><br>
Visit our website for complete information.<br><br>
Enjoy a summer evening concert on Main Street at 8pm. Doors and cash bar open at 7pm.<br><br>Look for more details and ticket sales to be released soon on our website<br>  <br><br>
<br>
</td>

但是,如果我在那个 td 上调用 text,我会得到 td 中的所有文本。我只想要不在任何元素内的最后一位。我尝试使用 XPath 和 parent 以便我可以说 "just give me the text that is inside the td (not nested inside of another element)" 但我无法让它工作。有人对此有任何想法吗?

我建议使用更灵活的 xpath。 如果我没理解错的话,你会喜欢:

I only want the last bit that is not inside any element

所以,试试这个 XPath:

//table//td[last()]/text()

试试这个代码:doc.css('td')[3].css('> text()').to_s.strip