How to analyse web with nokogiri?

how to analyse web with nokogiri?
there is my html web:

date 2009-12-31 2009-09-30
asset 580 680
cash 1,693 1,777
finance 201 2497
note 500 700
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

Le 10/04/2010 16:07, Pen T. a écrit :

680 201

Yes it’s possible you could try this :

table = "

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

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;”

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 #{ } ?

1
css(“table tr td”) will create an array of all td elements of the

I've passed to Nokogiri::HTML() method, that's why I can call the each metho on the result

2
You’v got a table with 3 columns (1 for title, and 2 for values) so i
use
the modulo (%, it’s a math function : 0%3=0; 1%3=1; 2%3=2; 3%3=0; 4%3=1
…)
function to place all the data at the right index
result[i%3] ||= “” means if result[i%3] is nil then put an empty string
into
it
#{} helps to include a string into another, here i use #{} to add a ; at
the
end of the string

2010/4/11 Pen T. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs