Sunday, February 25, 2018

RUBY Nokogiri CSS HTML Parsing



I'm having some problems trying to get the code below to output the data in the format that I want. What I'm after is the following:




CCC1-$5.00
CCC1-$10.00
CCC1-$15.00
CCC2-$7.00




where $7 belongs to CCC2 and the others to CCC1, but I can only manage to get the data in this format:





CCC1-$5.00
CCC1-$10.00
CCC1-$15.00
CCC1-$7.00
CCC2-$5.00
CCC2-$10.00
CCC2-$15.00
CCC2-$7.00




Any help would be appreciated.



require 'rubygems'  
require 'nokogiri'
require 'open-uri'


doc = Nokogiri::HTML.parse(<<-eohtml)










CCC1








$5.00
$10.00
$15.00










CCC2






$7.00



eohtml


doc.css('td.BBB > span.CCC').each do |something|
doc.css('tr > td.EEE, tr > td.FFF').each do |something_more|
puts something.content + '-'+ something_more.content
end
end

Answer



How about this?



doc.css('td.BBB > span.CCC').each do |something|

something.parent.parent.css('tr > td.EEE, tr > td.FFF').each do |something_more|
puts something.content + '-'+ something_more.content
end
end

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...