I want to use nokogiri to loop through a html and create an object corresponding to every row. I am able to define the root xpaths where I want the data to fill the object varibles comes from but I dont know how to group these as an object.
My code is below. I know it doesn't work but I dont know what direction to go to make it work.
require 'rubygems'
require 'nokogiri'
doc = Nokogiri::HTML.parse(<<-HTML_END)
"
LV1LV2LV3
MV1MV2MV3
NV1NV2NV3
"
HTML_END
class Post
def initialize(v1, v2, v3 )
@v1 =v1
@v2 = v2
@v3 = v3
end
def v1= (v1)
@v1 =v1
end
def v2
@v2 =v2
end
def v3
@v3 =v3
end
end
class PostList
def initialize
@posts = Array.new
end
def append(aPost)
@posts.push(aPost)
self
end
def deleteFirst
@posts.shift
end
def deleteLast
@posts.pop
end
end
list = PostList.new
parent = doc.css('body').first
gets the contects of the row
parent.xpath("//div/table[@class='ipbtable']/tr" ).each do |a_tag|
k1 = "x"
k2 = "x"
k3 = "x"
a_tag.xpath("td[1]").each do |x_tag|
puts x_tag.content
end
list.append(Post.new(k1, k2, k3) )
end
Best Answer
The main problem with the code appears to be you're passing strings (
'K1', 'K2', 'K3'
) that vaguely resemble the names of variables rather than the variables themselves (k1, k2, k3
). However, you could express this more succinctly as:This simply loops over each row and creates a Post using the textual content of each td in the row.