1

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

4

1 回答 1

4

代码的主要问题似乎是您传递的字符串 ( 'K1', 'K2', 'K3') 模糊地类似于变量的名称,而不是变量本身 ( k1, k2, k3)。但是,您可以更简洁地将其表达为:

doc.search('table > tr').each do |row|
  properties = row.search('td/text()').collect {|text| text.to_s}
  list.append Post.new(*properties)
end

这只是遍历每一行并使用该行中每个 td 的文本内容创建一个 Post。

于 2009-07-12T09:32:31.213 回答