# How to analyse web with nokogiri?

**URL:** <https://www.ruby-forum.com/t/how-to-analyse-web-with-nokogiri/186651>\
**Category:** Ruby\
**Created:** [April 10, 2010, 4:07pm UTC](https://www.ruby-forum.com/t/how-to-analyse-web-with-nokogiri/186651 "2010-04-10T16:07:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [April 10, 2010, 4:07pm UTC](https://www.ruby-forum.com/t/how-to-analyse-web-with-nokogiri/186651/1 "2010-04-10T16:07:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [April 10, 2010, 4:56pm UTC](https://www.ruby-forum.com/t/how-to-analyse-web-with-nokogiri/186651/2 "2010-04-10T16:56:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [April 11, 2010, 3:54am UTC](https://www.ruby-forum.com/t/how-to-analyse-web-with-nokogiri/186651/3 "2010-04-11T03:54:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [April 11, 2010, 1:30pm UTC](https://www.ruby-forum.com/t/how-to-analyse-web-with-nokogiri/186651/4 "2010-04-11T13:30:03Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
