2011-11-04 73 views
0

表列我有一個HTML文檔用很簡單的表像這樣解析3角度來說,Hpricot

<table> 
<tr><th>Country</th><th>Date</th></tr> 

<tr> 
    <td><b><a href="/calendar/?region=BE">Belgium</a></b></td> 
    <td align="right"><a href="/date/04-20/">20 April</a> <a href="/year/2001/">2001</a></td> 
    <td>(original release)</td> 
</tr> 

<tr> 
    <td><b><a href="/calendar/?region=BE">Belgium</a></b></td> 
    <td align="right"><a href="/date/04-25/">25 April</a> <a href="/year/2001/">2001</a></td> 
    <td></td> 
</tr> 

<tr> 
    <td><b><a href="/calendar/?region=FR">France</a></b></td> 
    <td align="right"><a href="/date/04-27/">27 April</a> <a href="/year/2001/">2001</a></td> 
    <td></td> 
</tr> 

<tr> 
    <td><b><a href="/calendar/?region=CH">Switzerland</a></b></td> 
    <td align="right"><a href="/date/05-25/">25 May</a> <a href="/year/2001/">2001</a></td> 
    <td>(French speaking region)</td> 
</tr> 

<tr> 
    <td><b><a href="/calendar/?region=CZ">Czech Republic</a></b></td> 
    <td align="right"><a href="/date/07-06/">6 July</a> <a href="/year/2001/">2001</a></td> 
    <td>(International Film Festival)</td> 
</tr> 
</table> 

前兩列易於解析:

document.search("a[@href*=calendar]").each { |country| countries << country.inner_text } 
document.search("td[@align*=right]").each { |date| dates << date.inner_text } 

但我有麻煩,從查找值第3欄。我需要所有這些,包括空白的。我怎樣才能做到這一點?

回答

0

回答我的問題:

document.search("td[@align*=right]").each { |comment| comments << comment.next.next.inner_text }