From: Pen Ttt on
how to analyse web with nokogiri?
there is my html web:
<table>
<tr>
<td><strong>date</strong></td>
<td>2009-12-31</td>
<td>2009-09-30</td>
</tr>

<tr>
<td><strong>asset</strong></td>
<td>580</td>
<td> 680</td>
</tr>

<tr>
<td><strong>cash</strong></td>
<td>1,693</td>
<td>1,777</td>
</tr>

<tr>
<td><strong>finance</strong></td>
<td>201</td>
<td>2497</td>
</tr>


<tr>
<td><strong>note</strong></td>
<td>500</td>
<td>700</td>
</tr>
</table>
can i get array,such as a with nokogiri?
a[0]=date,asset,cash,finance,note
a[1]=2009-12-31,580 ,1,693,201,500
a[2]=2009-09-30,680,1777,2497,700
--
Posted via http://www.ruby-forum.com/.

From: AMILIN Aurélien on
Le 10/04/2010 16:07, Pen Ttt a écrit :
> <table>
> <tr>
> <td><strong>date</strong></td>
> <td>2009-12-31</td>
> <td>2009-09-30</td>
> </tr>
>
> <tr>
> <td><strong>asset</strong></td>
> <td>580</td>
> <td> 680</td>
> </tr>
>
> <tr>
> <td><strong>cash</strong></td>
> <td>1,693</td>
> <td>1,777</td>
> </tr>
>
> <tr>
> <td><strong>finance</strong></td>
> <td>201</td>
> <td>2497</td>
> </tr>
>
>
> <tr>
> <td><strong>note</strong></td>
> <td>500</td>
> <td>700</td>
> </tr>
> </table>
>
Yes it's possible you could try this :

table = "<table>
<tr>
<td><strong>date</strong></td>
<td>2009-12-31</td>
<td>2009-09-30</td>
</tr>
<tr>
<td><strong>asset</strong></td>
<td>580</td>
<td> 680</td>
</tr>
<tr>
<td><strong>cash</strong></td>
<td>1,693</td>
<td>1,777</td>
</tr>
<tr>
<td><strong>finance</strong></td>
<td>201</td>
<td>2497</td>
</tr>
<tr>
<td><strong>note</strong></td>
<td>500</td>
<td>700</td>
</tr>
</table>"

parser = Nokogiri::HTML(table)
result = [ ]
parser.css("table tr td").each_with_index do |td, i|
result[i%3] ||= ""
result[i%3] << "#{td.content};"
end

puts result
>>"date;asset;cash;finance;note;"
"2009-12-31;580;1,693;201;500;"
"2009-09-30; 680;1,777;2497;700;"

--
Aurélien AMILIN


From: Pen Ttt on
think AMILIN Aurélien ,i get it
i am a beginner,there are some problems :
1\
what is the meaning of css("table tr td")?

2\
what is the meaning of
result[i%3] ||= "" #let nil be result array??
what is i%3 and || ?
result[i%3] << "#{td.content};" #td.content is the td value,i know
what is #{ } ?






--
Posted via http://www.ruby-forum.com/.