English 中文(简体)
缩略语
原标题:CSS selectors and conditionals in a ruby script

我试图用诺科吉里和科安会的挑选者在鲁比拉撰写一篇文章。 不过,我对文字中的附加条件略感困惑。 这里,我迄今为止(page 是使用Nokogiri下载的html):

page.css( h3 ).each do |node|
  puts node.text
end

page.css( li ).each do |node|   
  if /d|D/.match(node)
    puts node.text.scan(/[d]+D*/).first
  end
end

page.css( td b ).each do |node|
  puts node.text
end

This all works fine. However, what I really want is something like this:

page.css( h3, li, td b ).each do |node|
  # if it s an h3 node, do one thing
  # if it s a li node, do another thing
  # else if it s a  td b  node, do another thing
end

这将使该网页能够按顺序排列,而不是经过三场不同的时间。 然而,我不敢确定如何在我的发言稿中写这些条件。

EDIT: So now my script is

page.css( h3, li, td b ).each do |node|
        case node.name
        when  h3 ,  b 
            puts node.text
        when  li 
            if /d|D/.match(node)
                puts node.text.scan(/[d]+D*/).first
            end
        else
            next
    end
end

然而,它并没有改变行为。 它按照此前的顺序处理这些内容(所有三个要素,即所有利要素,即所有要素)。

EDIT 2:

奥凯,我最后去工作。 这里是我的最后一套条件:

page.traverse do |node|
    case
            when  h3  == node.name 
            puts node.text
        when  li  == node.name 
            puts node.text.scan(/[d]+D*/).first if /d|D/.match(node)
        when  b  == node.name
            puts node.text if (node.parent.name ==  p  or node.parent.name ==  td )
    end
end

感谢!

最佳回答

你们也许会寻求反常:

page.traverse do |node|
  case
    when [ h3 ,  li ].include?(node.name) then puts node.text
    when  b  == node.name &&  td  == node.parent.name then puts node.text[/d+D*/]
  end
end
问题回答

Nokogiri的网页将不隔三次,即便在您的首例情况下也是如此。 Nokogiri一带一页,在网上创建OM,然后利用OM来研究你想要的节点。 开展多个社区服务或卫生调查并不效率低。

然而,如果你仍然想一劳永逸地 no,你可以做到:

page.css( h3, li, td b ).each do |node|
  case node.name
  when  h3 
    do_something
  when  li 
    do_something_else
  when  b 
    do_another_thing
end

请注意,如果您需要区分<代码>td b和p b,那么这一技术就赢得了工作。 d 我建议进行单独调查。





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签